The CLASSPATH variable contains a list of directories where class files are found. A . jar file is really a zipped up directory, so the name of the . jar file itself should be in the CLASSPATH, not the name of the directory it is in.
To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]
-classpath pathSpecifies the path javac uses to look up classes. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by colons. It is often useful for the directory containing the source files to be on the class path.
Try the following:
java -cp jar1:jar2:jar3:dir1:. HelloWorld
The default classpath (unless there is a CLASSPATH environment variable) is the current directory so if you redefine it, make sure you're adding the current directory (.) to the classpath as I have done.
In windows:
java -cp C:/.../jardir1/*;C:/.../jardir2/* class_with_main_method
make sure that the class with the main function is in one of the included jars
Note for Windows users, the jars should be separated by ;
and not :
.
for example:
javac -cp external_libs\lib1.jar;other\lib2.jar;
Use the -cp
or -classpath
switch.
$ java -help
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
...
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
(Note that the separator used to separate entries on the classpath differs between OSes, on my Windows machine it is ;
, in *nix it is usually :
.)
javac HelloWorld.java -classpath ./javax.jar , assuming javax is in current folder, and compile target is "HelloWorld.java", and you can compile without a main method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With