This question has been asked before but I still cannot figure whats wrong for some reason.
I got a class named NewClass in package syntaxtest in file src. From src path I type :
javac src/syntaxtest/NewClass.java
and the class is compiled and I can see NewClass.class in syntaxtest folder. Now from that same path or even the same folder with NewClass.class, I can't figure out how to run the class from terminal. I have made many different attempts but ether I get
ClassDefNotFound or ClassDefNotFound (wrong name : syntaxtest/NewClass)
Try "java -cp src syntaxtest.NewClass".
That is, if you have a folder "src" which contains the subfolder (package) "syntaxtest" and the class "NewClass" is in "package syntaxtest", then the above command will work.
$ ls src/syntaxtest
NewClass.java
$ cat src/syntaxtest/NewClass.java
package syntaxtest;
public class NewClass {
public static void main(String args[]) {
System.out.println("Hello, World!");
}
}
$ javac src/syntaxtest/NewClass.java
$ java -cp src syntaxtest.NewClass
Hello, World!
I've made the following test:
Created a java file in home/test/blah/TestClass.java
package blah;
public class TestClass {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Went to directory home/test/
Compiled the file by typing:
javac blah/TestClass.java
Got the file compiled ok.
Ran it by typing:
java blah.TestClass
Got the message "Hello World!" as expected: program runs ok.
Went to directory home/
Tried to run by typing:
java test/blah.TestClass
... and many other combinations of slashes and dots..... nothing worked.... keep getting the same Exception as you:
java.lang.NoClassDefFoundError
So it seems to me that to run a Java class using the command 'java' you really must be in the application's root folder.
I had a similar problem.
I wanted to organize my project using a src and bin folders directly from the Mac finder and use Emacs or some other text editor. I just didn't like eclipse.
You can't execute the classes from another folder, but you can do is compile from another folder into the one you are going to execute.
For example (assuming there is no package), move to the bin folder and run:
$ javac ../src/name.java -d ../bin/
(That compiles from the src folder and outs the .class file directly on bin)
Related
I cannot run the following program from command line the usual way:
package animal_package;
public class my_animal {
public static void main(String[] args) {
System.out.println("Hello animal");
}
}
E.g. from Command prompt: I go to "D:\Java\src\animal_package" where my java program is and compile it:
D:\Java\src\animal_package>javac my_animal.java
D:\Java\src\animal_package>java my_animal
Error: Could not find or load main class my_animal.java
I have looked on Google and came around class path problem but couldn't make any sense from it all.
Which command line would be correct in my case?
To compile:
javac my_animal.java
To run (from src directory):
java animal_package.my_animal
cd .. then java -cp . animal_package.my_animal. The package is part of the fully qualified class name. Or,
java -cp .. animal_package.my_animal
You can compile the class either from src directory using:
javac animal_package\my_animal.java
OR from animal_package directory using: javac my_animal.java
To run the program from src directory use
java animal_package.my_animal OR java -cp . animal_package.my_animal
I exported a jar file from eclipse and in the jar file are various packages and sub packages with java class files.
I'd like to run one of these nested class files on the command line in Windows.
The class has a main and I am using the following to try to run it,
java -classpath .;./example.jar example
Note that example is the name of the class as well as the jar.
I've also tried to spell out the full path of the class
java -classpath .;./example.jar the.whole.path.example
How can I run the example class?
EDIT:
OK this is kind of stupid, the full path was incorrect. I checked this over numerous times yet it was only when I came back to it that I noticed the error.
Just running java -cp example.jar the.whole.path.example should do the trick. If not, then something with your JAR file is wrong. The class name must be fully qualified (with package name) and the specified class must have a correct main method:
public static void main(String[] args) {
}
Without seeing any of your code or any output (hint, hint) it's hard to say what's happening, but this works for me:
$ cat x/y/z/A.java
package x.y.z;
public class A
{
public static void main(String args[]) throws Exception
{
System.out.println("here in A");
}
}
$ javac x/y/z/A.java
$ jar cvf a.jar x/y/z/A.class
added manifest
adding: x/y/z/A.class(in = 459) (out= 311)(deflated 32%)
$ java -classpath a.jar x.y.z.A
here in A
And in case the poster or someone reading this in the future isn't familiar with Unix, the lines starting with $ are commands I type into the shell and everything else is output from those commands. Eclipse will take care of the first three for you, then the final java -classpath a.jar x.y.z.A is the command to execute the main method in the x.y.z.A class.
Check if your assumptions are correct. Show the structure of your jar and compare it to what you try to run:
jar tf example.jar
If the jar contains a lot of entries you might want to grep / find the relevant class:
jar tf example.jar | find /i "Example"
I want to run a Java file with the following source code:
package u0a1;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
To run the file I did the following things:
C:\.. \u0\u0a1> javac HelloWorld.java (this works, class file is created)
Then I try to run it with:
C:\..\u0> java u0a1.HelloWorld
This step doesn't work. Main class could not be found.
I also tried
C:\..\u0\u0a1> java HelloWorld
C:\..\u0> java u0a1\HelloWorld
None of them worked.
This is a piece i found somewhere else, worked for me.
Have you set your JAVA_HOME correctly? If not you have to work with
the full path
Example: "C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe"
HelloWorld.java
If you have runtime issues, you should work it out like this
Select MAIN directory - not package directory
java u0a1/HelloWorld
If you have problems with CLASSPATH or JAVA_HOME - try this:
"C:\Program Files\Java\jdk1.7.0_51\bin\javac.exe" HelloWorld.java
source: http://quandano.com/questions/how-to-run-a-java-file-within-a-package-from-cmd
You are compiling a program of package , so it should compile this way
C:.. \u0\u0a1> javac -d . HelloWorld.java
here -d for creating package u0a1
and "."
for from current working directory
after compiling this way a folder will create with name "u0a1"
then other thing will work properly
I created little HelloWorld example and I am having problem running it from command prompt (on Windows). When I attempt to run it by:
java tcpServer from command prompt I get NoClassDefFoundError
I am able to compile it with javac, and class file gets generated.
Somewhere I was reading that I have to put path to my class folder into CLASSPATH environment variable. I've done it and rebooted machine, but I still get the same error.
I also was trying to run it by java -cp c:\MyFolderWhereClassFileIs HelloWorld, it doesn't work.
I've looked into ENV variables and I have following:
JAVA_HOME: C:\Program Files (x86)\Java\jdk1.6.0_26;
JRE_HOME:C:\Program Files (x86)\Java\jre6;
CLASSPATH: C:\HelloWorld;
So, how do I run this?
Any ideas how to resolve this? Thanks.
PS. The most annoying thing to me is, if I create java project in eclipse, and create HelloWorld example, then it runs fine...
UPDATE:
Here is the code. It does have package specified.
package test.com;
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello World");
}
}
My HelloWorld.java and HelloWorld.class files are here:
C:\workspace\TestApp\src\test\com
One thing I learned so far is that I can't run it from within com folder or test folder. I have to be in src folder class file can be found... but I am still not able to run it... always the same error.
Does your class have a package name? That is, a statement at the top which reads
package <mypkgname>;?
If so, then you have to (A) create the correct directory structure and (B) prefix the class name with the package name in your java command.
For example, if your Java file looks like this:
package hello;
public class HelloWorld {
...
}
Then a basic directory structure would resemble:
src/hello/
src/hello/HelloWorld.java
src/hello/HelloWorld.class
And your bash command would be:
cd src
java hello.HelloWorld
Otherwise, if your class has no package name, it will be placed in the default package. In this case, you simply have to cd to the directory where your class file resides, and run java HelloWorld.
I'm trying to learn to run java apps from windows command line and I can't figure out one problem.
I have a simple class on my desktop:
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
If I run javac and java commands when I'm in my desktop directory in cmd everything is well, but if I go one directory back (so I won't be in the same directory as the .java and .class files) then my cmd directory is C:\Users\Tomas and my Hello.java and Hello.class files are in C:\Users\Tomas\Desktop. I can run the command javac Desktop\Hello.java and it works, but then if I try to do java Desktop\Hello.java I get an Exception in thread "main" java.lang.NoClassDefFoundError: Desktop\Hello (wrong name: Hello).
I know that NoClassDefFoundError is throws when a class was available at compile time, but ClassLoader can't find it during run time ( found a good article about it here).
I think the problem has something to do with the CLASSPATH variable, so I set it to:
"C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop"
(I included "C:\Users\Tomas\Desktop" just to try everything)
And I tried running the "java" command with -classpath and -cp options:
java -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop" Desktop\Hello
And I keep getting the same exception.
I't would be great if someone can explain my error and why this is happening, and maybe point even give some directions where can I read more about this.
Thank you.
class
package Desktop;
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
compile (here Desktop means standart windows directory)
javac Desktop\Hello.java
execute (here Desktop means package. Desktop/Hello is fully class name)
java Desktop/Hello
java -classpath 'C:\Users\Tomas\Desktop\Hello.class'
Should run it.
Try java -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop" Hello
I only removed Desktop from your class name.