I have both a library.jar and program.jar in Java folder.
What is the correct command line to run? One method I tried is:
C:>java -cp c:\java\library.jar;.\java\program.jar program [param]
Try
java -cp c:\java\library.jar;.\java\program.jar package.the.MainClass [param]
From http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
Folders and archive files
When classes are stored in a directory
(folder), like
c:\java\MyClasses\utility\myapp, then
the class path entry points to the
directory that contains the first
element of the package name. (in this
case,C:\java\MyClasses, since the
package name is utility.myapp.)
But when classes are stored in an
archive file (a .zip or .jar file) the
class path entry is the path to and
including the .zip or .jar file. For
example, to use a class library that
is in a .jar file, the command would
look something like this:
C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
Multiple specifications
To find class files in the directory
C:\java\MyClasses as well as classes
in C:\java\OtherClasses, you would set
the class path to:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ...
Note that the two paths are separated
by a semicolon.
If you intend for your program.jar to be an executable JAR, you'll have to run it this way (and read this):
java -jar program.jar
Classpath entries can also contain the wildcard(*) character. For example, the class path entry C:\java\* specifies all JAR files in the C:\java directory and will be expanded into C:\java\library.jar;C:\java\program.jar.
Related
I have a class file Main.class which needs a JAR file abc.jar to run.
Both files are in the same directory. Now I try to run the class file with
java -cp "." Main
but I get a java.lang/NoClassDefFoundError.
I thought -cp "." tells the classpath to include the current directory, but somehow it doesn't.
How do I get this JAR file in the current directory on the class path?
Thanks to patrinox' comment I figured it out:
The JAR itself needs to be in the CLASSPATH property, not only the directory containing the JAR. Therefore the command line has to read:
java -cp ".:./abc.jar" Main
I want to run a java project in windows. I first compiled the .class file in linux. Copy back to windows. Now under the path H:\deletefiles has delete.class, delete.java, a.jar, b.jar. The package for class delete is deleteFiles.
My java class path is C:\program Files\Java\jre7\bin, Where I have no access to write.
I run in command prompt C:\program Files\Java\jre7\bin>
java -cp H:\deleteFiles\deleteFiles.delete
always has the problem could not find or load main class, what's the problem? thanks
You are missing the actual class to be run. The -cp H:\deleteFiles\deleteFiles.delete only defines the classpath to be used, but not which class you want to run (and you limit the classpath to a single class as well).
What you want is:
java -cp H:\deleteFiles\deleteFiles delete
Note the blank (space) between H:\deleteFiles\deleteFiles which means you are passing two parameters to the java command:
-cp H:\deleteFiles\deleteFiles - the classpath to use
delete - the class to run
If you need the classes that are part of the jar files, you need to add them to the classpath as well:
java -cp H:\deleteFiles\deleteFiles;H:\deleteFiles\deleteFiles\a.jar;H:\deleteFiles\deleteFiles\b.jar delete
You need to set the classpath to the location which contains the package hierarchy. If your package is named deleteFiles the location needs to contain a directory named deleteFiles which contains the class file.
In your example you would run it with
java -cp H:\ deleteFiles.delete
you should call delete.class in your java command line, like this:
java -cp H:\deleteFiles\delete
To execute a Java program you have two options. Using a class file or using a jar file.
If your program only contains a single source file, executing the class file would be fine. But if you have multiple sources, you would have to copy all of them. Then a jar would be more practicable.
For class:
java -cp <class path> <class name>
For jar (if the main class is set):
java -jar <jar file>
I wonder if it's possible to run a program without packaging it into a jar.
For instance we have this:
-AppRoot
Main.class
-Misc
Math.class
OtherTools.class
-YetAnotherFolder
UsefulFunctions.class
Is this possible? The main method should be executed from command line or similar.
You can run this way from the AppRoot directory
javac -cp Misc/*:YetAnotherFolder/* Main.java //To compile
java -cp Misc/*:YetAnotherFolder/* Main // To run
Below is some documentation
-classpath classpath
-cp classpath
Specifies a list of directories, JAR archives, and ZIP archives to search for class files. Class
path entries are separated by colons (:). Specifying -classpath or -cp overrides any setting of the
CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the cur-
rent directory (.).
Add all (sub-)directories containing class files to classpath and use the class with the main method as argument of the java executable.
The directory structure is your package structure.
java -cp ./:./AppRoot:./AppRoot/Misc:./AppRoot/YetAnotherFolder AppRoot.Main
This should work if all dependencies are resolved and on the classpath.
I was working on the Stanford sentiment classifier on windows. I wanted to retrain my own model, and here's how it was specified on the website:
java -mx8g edu.stanford.nlp.sentiment.SentimentTraining -numHid 25 -trainPath train.txt -devPath dev.txt -train -model model.ser.gz
But this gave me the error:
could not find or load main class
But on changing it to java -cp "*" it worked.
Class path entries can contain the basename wildcard character ,
which is considered equivalent to specifying a list of all the files
in the directory with the extension .jar or .JAR. For example, the
class path entry foo/ specifies all JAR files in the directory named
foo. A classpath entry consisting simply of * expands to a list of
all the jar files in the current directory.
From Oracle Docs
-cp < class search path of directories and zip/jar files>
Search all jar and zip files in the current directory for a given class file
The cp flag specifies the classpath, that is, which other archives are to be considered for this program.
Usually, you'd give it a colon-separated list of jar files, but this particular example is a special case according to the documentation:
If -classpath and -cp are not used and CLASSPATH is not set, then the user class path consists of the current directory (.). As a special convenience, a class path element that contains a base name of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. A Java program cannot tell the difference between the two invocations.
Note that the quotes are necessary, beause otherwise the shell would exapand it to all the files in the directory rather than only jar files.
HI guys,
There is an abc.jar under /tomcat_lib. I need use this in my def.java
I tired
javac -classpath /tomcat_lib/ -d
../classes def.java
but it doesn't work
But if it works if I use
javac -classpath /tomcat_lib/abc.jar
-d ....
Can anyone help explain it?
To add a jar to your classpath, you need to specify the path up to and including the .jar file.
Quoting the official Java SE 6 documentation at Oracle.com:
Each [item in your classpath] should
end with a filename or directory
depending on what you are setting the
class path to:
For a .jar or .zip file
that contains .class files, the class
path ends with the name of the .zip or
.jar file.
For .class files in an
unnamed package, the class path ends
with the directory that contains the
.class files.
For .class files in a
named package, the class path ends
with the directory that contains the
"root" package (the first package in
the full package name).
...and from the "Folders and Archive Files" section of the same documentation:
When classes are stored in a directory
(folder), like
c:\java\MyClasses\utility\myapp, then
the class path entry points to the
directory that contains the first
element of the package name. (in this
case, C:\java\MyClasses, since the
package name is utility.myapp.)
But when classes are stored in an
archive file (a .zip or .jar file) the
class path entry is the path to and
including the .zip or .jar file.