I use a jar file called korat.jar. I executed with the command line:
java -cp $CLASSPATH korat.Korat --visualize --class test.Add --args 3
The classpath contains the path of the jar and also the Add.class file.
I want to execute this jar in my own program java in netbeans IDE. I think I would use:
String []s={test.Add.class.getName(),"--visualize","--class","test.Add","--args","3"};
Korat.main(s);
I get this exception: java.lang.NoClassDefFoundError
What do you mean "to execute the jar in my own program"? The jar contains some classes, if they are in your class path, you can instantiate the classes themselves and invoke some methods. In that case, you should use test.Add class. But it seems like the class is not in your classpath - java.lang.NoClassDefFoundError.
Related
I need to compile and run simple code using the gson library, but I can't use Maven, Gradle or the IDE.
The directory contains Main.java and gson-2.9.0.jar
javac -cp gson-2.9.0.jar Main.java works correctly and creates Main.class
But when I run java -cp ./*: Main, I get
Error: Could not find or load main class Main.
Caused by: java.lang.ClassNotFoundException: Main
I also tried the following commands:
java -cp gson-2.9.0.jar Main
java -cp gson-2.9.0.jar: Main
java -cp ./gson-2.9.0.jar:./* Main
But all these commands give the same result. I've never had to run code from the command line without Maven or IDEA before, so I think it's the classpath specification that's the problem. What am i doing wrong here?
If your main class is in a package (has a package ... declaration), you need to include the package name in the java call, e.g. java -cp ... mypackage.Main.
Additionally, the java documentation says about -cp / --class-path:
As a special convenience, a class path element that contains a base name of an asterisk (*) is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.
Therefore, using * for .class files does not work; instead you have to specify the directory name. Based on your question it looks like you are using Linux, and that the .class file and the JAR are in the same directory, so the following should work in your case:
java -cp gson-2.9.0.jar:. Main
(note the . after the :, indicating to include the current directory for the classpath)
I am making a maven project which contains a dependency with runtime scope. I want to call this jar class directly from command line. I don't have the option to make fat jar. My Jar structure is:-
one.jar
--- temp.class
.....
.....
/lib/two.jar
/lib/three.jar
suppose I want to call Temp1 class of two.jar directly from the command line.
you could place a "proxy" class in one.jar which delegates to the target class in your embedded two.jar
java -cp "path" "classname"
eg : java -cp C:\one.jar com.test.main.temp
OR
java -cp /usr/local/jar/one.jar:/some/other/two.jar com.your.main.classname
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>
So I'm still a noob in Java and I'm experimenting around with a few things.
I recently created a .jar file for my class using jar cvf <name>.jar <source files> and then used that jar to compile my driver class (javac -cp <name>.jar Driver.java) though how do I now run that class using the jar?
I've tried the following 2 commands:
java Driver and,
java -cp <name>.jar Driver.
The first gives me a NoClassDefFoundError for the class used, whereas the latter just gave me a single line error.
Error: Could not find or load man class Driver
What am I doing wrong? Is it possible I'm confusing this for something else?
I'm trying to do as much as I can without the use of any IDE.
You should put jar file and compiler output into classpath and specify main class:
java -classpath "<name.jar>;classes" Driver
EDIT (thanks to Kayaman):
If you are running command from linux/unix you have to use ":" as separator (in Windows works ";"). "classes" is a path to folder containing compiler output.
When creating an executable jar ( jar which contain a class with the main method) you should tell the jar which is the mainClass to be executed and for that you should create a file called 'Manifest.mf'.
The file should contain this:
Main-Class: MyPackage.MyClass
And when creating the jar you should use this to include your manifest:
jar cfm MyJar.jar Manifest.mf MyPackage/*.class
And for launching your jar :
java -jar MyJar.jar
I'm trying to run a simple piece of RMI code to be implemented in a larger project sooner. But when I put my server inside a jar and try to start it, I get an error; could not find class.
The command i used is java -cp myClassPath -Djava.rmi.server.codebase=file:C:\help\me\pls package-name.Logger
So I'm pretty sure my issue is with the very last part, how do i point to the class file insider a jar file?
But when I put my server inside a jar and try to start it, I get an error; could not find class. The command i used is java -cp myClassPath -Djava.rmi.server.codebase=file:C:\help\me\pls package-name.Logger.
There is nowhere here where you've tried to start a JAR file. You haven't even mentioned it on the command line.
The correct way to start a JAR file is to include a manifest with a Main- Class attribute, a Class-Path attribute if there are other JARs that this one depends on, and the command line
java -jar MyJar.jar -Djava.rmi.server.codebase=file:C:\help\me\pls