How to set a temporary Java classpath in Ubuntu
I am 开发者_运维技巧writing a Java program and I need to set a temporary classpath that includes my package. The package is on my Ubuntu desktop, which I am importing as /home/gaurav/Desktop. Do you have any idea how to set the Java CLASSPATH temporarily?
You set the Java classpath on Ubuntu the same way as you do on any Linux / UNIX platform (or indeed on Windows ... modulo some minor syntactic differences). There are two ways:
$ java -cp <classpath> some.ClassName arg1 arg2 ...
or
$ export CLASSPATH=<classpath>
$ java some.ClassName arg1 arg2 ...
where <classpath>
is a sequence of pathnames with ':' separators.
For more details refer to the manual entry for the 'java' command; e.g. here and here.
If you don't understand export CLASSPATH=...
read the Ubuntu manual entry for bash
, paying attention to what it says about setting variables, environment variables, and the export
built-in shell command. (Hint: $ man bash
.)
This is temporary. To make it permanent, add the line to the relevant shell init script; see man bash
for details.
how do i get details for my path which path i have set
The classpath is a list of pathnames of directories and JAR files that you want the JVM to search in order to find the classes it needs to run your application. You'll need to figure that out yourself ... or (re-)read the documentation of whatever it is you are trying to run.
If you want to run a java program from the desktop, you have three choices.
The easy choice is to write a small shell script and put that on the desktop. The smallest example might be:
#!/bin/sh
java -cp YOUR_CLASSPATH YOUR_CLASS_NAME "$*"
Next comes the use of 'jarjar' or 'shade' to make one big jar containing all your dependencies, and then run that with java -jar. (As a sub-option, if it's really just for you, you can make a jar with a META-INF/MANIFEST.MF containing a class path with absolute pathnames.)
The more complex choice is to learn to use JNLP to build a launchable item.
IIRC you can control the classpath with either an environment variable or a command-line option to java.
精彩评论