I am having some issues running my compiled java code from the command line. I have written it and compiled it using the IntelliJ IDE (where everything runs fine if done within the IDE), but wish to now run it from the command line.
Compiling from the command like (using javac) also works fine, but running (with java) does not.
I am almost certain this is a classpath issue but cannot seem to fix it. From my searching prior to posting this I found a post telling me to run the "set PATH=\%PATH\%;"C:\Program Files\Java\jdk1.6.0_21\bin" command and then try running java. I have also tried various arguements I have found for -cp and -classpath. The error is :
Exception in thread "main" java.lang.NoClassDefFoundError: Share/class
Caused by: java.lang.ClassNotFoundException: Share.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Share.class. Program will exit.
You're doing:
java -cp ... Share.class
Do
java -cp ... Share
Or if it's in a package
java -cp ... path.to.Share
You should not be supplying the class file as an argument, you should be supplying the fully qualified class name.
If your class is in the current directory and uses the default (empty) package, it will just be
java -cp . Share
or
java Share
The classpath is not used to point to the java executable, it's used to point to the various directories/jar files which contain your class files (at the root of the package structure).
See also
java - the Java application launcher (manual for invoking java)
Related
How to run a java file that has external jars in Linux. Also how to run it in Windows?
I tried the following, but not working.
javac -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application.java
thanks
------EDITED------
Now my class file got generated after compiling without any error. But when i run the file its showing following error
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar C:/com/example/Application
Exception in thread "main" java.lang.NoClassDefFoundError: C:/com/example/Application
Caused by: java.lang.ClassNotFoundException: C:/com/example/Application
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Any suggestion...
If you are running on linux, then there should be different directory structure from windows. Also the classpath on linux should contains paths separated path by colon(:) instead of semi-colon(;)
Put your jars in a directory on linux machines, may be in /var/tmp/myjars. And then compile your code using the below command:
javac -cp /var/tmp/myjars/lib1.jar;/var/tmp/myjars/lib2.jar Application.java
EDIT
As you have updated your question. You need to run your class on windows as mentioned here. I am taking few assumptions here that your Application class is in com.example package. If that is the case then you can run it from the folder containing the com folder as mentioned here:
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar com.example.Application
javac is the Java compiler. after compiling the program the running on windows should work like this
java -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application
You can try this
java -cp classpath=%classpath%;< jar-file full-path>; ClassName
Ex: java -cp classpath=%classpath%;c:/lib/lib1.jar;c:/lib/lib2.jar com.ab.cd.MyClass
I've been trying to execute the commands to run the RMI Hello world example but I Failed!
My execution steps are taken from here: http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html
The commands are:
javac: works fine and I get the class files
rmiregistry &: I get something like [1] 17122
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ example.hello.Server:
Gives me an error message
Exception in thread "main" java.lang.NoClassDefFoundError:
example/hello/Server Caused by: java.lang.ClassNotFoundException:
example.hello.Server at
java.net.URLClassLoader$1.run(URLClassLoader.java:202) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:190) at
java.lang.ClassLoader.loadClass(ClassLoader.java:306) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I did solve this problem by running each command on a new terminal window.
First I started with javac for the .java files, then run the command rmiregistry &.
Second I opened a new terminal window and run the command java for the Server class.
Third I opened a new terminal window and run the command java for the Client class.
and it works with me!
On the Mac, the Users directory in the root directory is written with a capital letter.
You wrote:
file:/users/ha/RMI/
file:/Users/ha/RMI/ would be correct. This applies only to file systems that have been setup explicitly to setup to ignore case (Case-insensitive). At least that is what the comments below are saying. I cannot verify this myself.
A classpath is also needed under many circumstances. It depends on your dependencies. But if you needed the classpath in windows, you will need a similar one on Mac.
Depending on where you .class files are, I would add the classpath to your invocation.
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ -cp=/Users/ha/RMI example.hello.Server
This assumes, of course, that the class files are under /Users/ha/RMI/example/hello
Just look inside of the directory to verify that the class files are there. It should be the directory that you designated after the javac command and -d argument.
I used three terminal windows for execution of a rmiregistry program.
1.Use this terminal window to compile all your files (javac filename.java) and use the command "rmiregistry &" this is the windows equivalent command for "start rmiregistry". (Note:'start' command doesn't work on the macOS)
2.Use this to run the java file ie. Server file (ie. java RMI_Server)
3.Use this to run the client file ie. Client file (ie. java RMI_Client 127.0.0.1)
And yeah it worked for me. (Note: The Directory should be set properly in the terminal)
http://pastebin.com/1btVw8Cb
There are two classes in the above code.
So above is my code which is working fine when I hit run in Eclipse, runs fine in Netbeans as well.
I am trying to create a standalone application, a jar file.
The error I get when I double my jar is:
Could not find the main class: NewJFrame. Program will exit.
I get the following from the command promt:
E:\Java Programs\Eclipse Workspace\test3\src\test3>java testT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: testT/jar
Caused by: java.lang.ClassNotFoundException: testT.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: testT.jar. Program will exit.
I followed the command from here: "Could not find the main class: XX. Program will exit."
So this is what I am typing in the command promt to create my jar:
E:\Java Programs\Eclipse Workspace\test3\src\test3>jar cfm MyJar.jar manifest.tx
t *.class SINGLE.TXT
http://imgur.com/a/wlCpc#SYR3L
Some shots above I took to show the process.
So I think the problem could be error when I do javac? But it builds fine and runs fine in netbeans and eclipse >< Please help.
You need to use the -jar flag.
java -jar MyJar.jar
http://docs.oracle.com/javase/tutorial/deployment/jar/run.html says
JAR Files as Applications
You can run JAR-packaged applications with the Java interpreter. The basic command is:
java -jar jar-file
The -jar flag tells the interpreter that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all the application-specific code.
and http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html explains how to put a manifest in the jar.
The m option indicates that you want to merge information from an existing file into the manifest file of the JAR file you're creating.
it is now working. I have not changed anything, maybe the memory got curroupted and that is why it was giving me problems. I think restart the computer is what fixed it. But yea, I don't know for sure what was wrong, though it is working fine now. So thank you for your help guys
Add
Main-class: Splash
part to Manifest file. set Main class name instead Splash
That will work...
I am just trying to compile and run a very simple test program, but it simply will not work, and I have no idea what the problem is.
I have a java project that's been heaped on me, and I know little to nothing about java. Especially compiling from the windows command line.
I have two Jars that I need to compile a simple "hello world" program with.
Here's my "build.bat"
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
Then, I:
C:\jdk1.6.0_21\bin\java sample
This spits back the error:
Exception in thread "main" java.lang.NoClassDefFoundError: sample
Caused by:
java.lang.ClassNotFoundException:
sample
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native
Method
at java.net.URLClassLoader.findClass(URLClassLoader.java:190
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
No matter how I set up my classpath, I cannot for the life of me get this HELLO WORLD program to run.
Can someone please help me out? I'm pulling my hair out.
You can also specify the classpath for the interpreter to locate your class:
java -classpath "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; Sample
This would run your class from any working directory.
Run your program from the directory with the class in it:
C:\Users\FREYERA\Desktop\Test\>java sample
CLASSPATH (normally) includes the current directory.
If you have "sample.class" in the current directory, and you also need classes in test1.jar and test2.jar, this should work:
java -cp "test1.jar;test2.jar;." sample
Afer running this command
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
It would have created a .class file at this location "C:\Users\FREYERA\Desktop\Test\". i.e. sample.class
You need to either go to this folder location and run your java command to execute the program. Make sure that your "JAVA_HOME" environment variable is set.
Or you can copy the sample.class file to "C:\jdk1.6.0_21\bin\" folder and run the command.
I'm working through a ANTLR (a language processing library) book and there are many examples that should be easy to compile using the command line.
Some information to get te problem:
antlr-3.2.jar contains the library classes. I added the antlr-3.2.jar to the CLASSPATH environment variable (Windows 7) and when compiling the classes with javac everything works fine.
This is what i execute to compile my program:
javac Test.java ExprLexer.java ExprParser.java
Test.java contains my main()-method whereas ExprLexer and ExprParser are generated by ANTLR. All three classes use classes contained in the antlr-3.2.jar. But so far so good. As I just said, compiling works fine.
It's when I try to execute the Test.class that I get trouble.
This is what I type:
java -cp ./ Test
When executing this, the interpreter tells me that he can't find the ANTLR-classes contained in the antlr-3.2.jar, altough I added an entry in the CLASSPATH variable.
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/runtime/Cha
rStream
Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.CharStream
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Test. Program will exit.
I'm using Windows 7 and Java 1.6_20. Can someone tell what is going on? Why will the interpreter not look in the jar-Archive I specified in the CLASSPATH?
I found some kind of workaroud. I copied the antlr-3.2.jar into the directory where the Test.class is located and then executed:
java -cp ./;antlr-3.2.jar Test
This worked out. But I don't want to type the jar-Archive everytime I execute my test programs. Is there a possibility to tell the interpreter that he should automatically look into the archive?
I'm using Windows 7 and Java 1.6_20.
Can someone tell what is going on? Why
will the interpreter not look in the
jar-Archive I specified in the
CLASSPATH?
-cp on the commandline overrides the CLASSPATH variable. There is no convenient way to do what you're trying to do. I'd suggest creating an ant script, shell script, or shell alias if you don't want to type out the full classpath each time.
Alternatively, you could put your Test application into its own jar file with a manifest that tells it to include antlr-3.2.jar in the classpath.