dyslabs
dyslabs

Reputation: 121

Java won't recognize classpath set by command line

Here is my code:

import java.net.URL;
import java.net.URLClassLoader;
public class App {
    public static void main(String[] args) {
        System.out.println("java.class.path="+System.getProperty("java.class.path"));
        ClassLoader cl = ClassLoader.getSystemClassLoader();

        URL[] urls = ((URLClassLoader)cl).getURLs();

        for(URL url: urls){
            System.out.println(url.getFile());
        }
    }
}

When I run this in Eclipse with the LWJGL and Slick2d libraries, I get, as expected:

java.class.path=/home/the-genius/workspace/classpath/bin:/home/the-geniu/workspace      
/libs/slick/lib/slick.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar  
/lwjgl.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl_util.jar:
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/jinput.jar
/home/the-genius/workspace/classpath/bin/
/home/the-genius/workspace/libs/slick/lib/slick.jar
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl.jar
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl_util.jar
/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/jinput.jar

However, when I export it as a runnable jar and execute via

java -cp /home/the-genius/workspace/classpath/bin:/home/the-geniu/workspace      
/libs/slick/lib/slick.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar  
/lwjgl.jar:/home/the-genius/workspace/libs/lwjgl/lwjgl-2.9.2/jar/lwjgl_util.jar: -jar app.jar

I get

java.class.path=classpath.jar
/home/the-genius/classpath.jar

Is there any reason for this to be happening? How do I fix it?

I'm running on Ubuntu, if that makes a difference. I've also tried it using both OpenJDK-7 and Sun Java-7.

Upvotes: 1

Views: 144

Answers (1)

kraskevich
kraskevich

Reputation: 18556

If you use -cp and -jar option at the same, the former is ignored. To fix it, you can either run it without -jar(add your jar file to the classpath and call the main class: java -cp app.jar App) or add the classpath to the jar manifest file.

Upvotes: 4

Related Questions