Ubuntu: Cannot set CLASSPATH



Question:

I'm using the command "java -cp /home/ubuntu/myapp" to set the command to the directory where mu app's classes are located. However I seem to be having no luck. The terminal simply answers by giving me a list of possible java commands - which funnily enough include "-cp". How do I get Ubuntu to take me seriously and actually do what I tell him?


Solution:1

Executive Summary

java -cp doesn't automatically run a program in the specified classpath, it just specifies the classpath to use when running a Java program which you must then specify in that same command.

Full Explanation

java runs Java programs. It does not modify them or store persistent configuration about them.

The -cp flag does not set the classpath, it specifies a classpath to use when running a java program.

Running java with only a -cp flag (and its argument) is invalid syntax. You must tell java what java program you want to run with that classpath. This will run that program, once, with that classpath. It will not cause it to be run with that classpath in the future unless the classpath is specified again.

It's unclear from your question exactly what you're trying to accomplish, but I've detailed a few possibilities below.

(If you still need more information, please feel free to edit your question for clarification and comment on this answer, or if you find you have a separate question, you can ask it separately. The manual page for the java command may also help; please note that the name of a class or jar file is required. To get the manual page for the exact installed version of java on your machine, run man java.)

You might not have to manually set a classpath at all.

If myapp is a directory located in /home/ubuntu, which contains classes for your program, then you don't (generally) have to specify a classpath at all. In that case, just run the program (i.e., pass the name of the class that contains the program's main function to java.

For example, if the .class file for the class that contains your main method is /home/ubuntu/myapp/launch-myapp.class, just run:

java /home/ubuntu/myapp/launch-myapp  

(Note that you don't specify the .class suffix, as it is implied.)

Most Java programs consist of multiple classes and often all the .class files are packed into a single .jar file. If that's the case, run java with the -jar flag and specify its full filename.

For example, if your program's classes (including the class containing main) is provided by the file /home/ubuntu/myapp/myapp.jar, just run:

java -jar /home/ubuntu/myapp/myapp.jar  

Note that, to run a .jar file with java -jar, you do specify the .jar extension at the end of the filename; it is not taken to be implied.

Much less frequently, you might really want to manually specify a classpath.

To do that, you must specify the the classpath and also what Java program you want to run (i.e., the class that contains its main method).

For example, suppose you want to run /home/bob/myapp.class with the classpath /home/ubuntu/myapp. Then you would run:

java -cp /home/ubuntu/myapp /home/bob/myapp  

Or if you're running /home/bob/myapp.jar with that classpath:

java -cp /home/ubuntu/myapp -jar /home/bob/myapp.jar  

You might be trying to set the default classpath, to be used when running any Java program in the future (except when an alternate classpath is specified).

If that's what you're trying to do, you should set the CLASSPATH environment variable. See the official documentation on that as pointed out by karel.

In short, you simply make CLASSPATH a semicolon-delimited list of the directories you want searched automatically.

For more information on setting environment variables, see this question and this Ubuntu documentation wiki page.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »