I have been trying to run any .class file from the command line, and I keep getting this error. After trying to figure out the problem on my own all night, i'm officially stumped.
I'm on linux, and I use eclipse usually so my .java file and .class file are in two different locations.
Heres classpath....
%> echo $CLASSPATH
/home/mike/Documents/java
Heres one of my .class directories...
/home/mike/Documents/java/homework/bin/src
Heres the corresponding .java directory...
/home/mike/Documents/java/homework/src/src
Heres what I enter in bash...
[ mike (23:31:16): ~] $ >java homework.bin.src.EulerMath
OR
[ mike (23:32:41): ~/Documents/java] $ >java -cp . homework.bin.src.EulerMath
OR
[ mike (23:34:08): ~/Documents/java/homework/bin/src] $ >java -cp . EulerMath
All give me the same error. Obviously im trying to run EulerMath.class, which is within the package src. This compiles and runs fine within eclipse, and I have a similar issue with every other program i try to run from the command line. I can compile the .java file with javac but when trying to run the new .class file i get this error. Id appreciate any help
2 Answers 2
Modify your CLASSPATH like
export CLASSPATH="$HOME/Documents/java/homework/bin"
then you can run it without a -cp command line like
java src.EulerMath
Without setting the CLASSPATH (or if you want to override the set CLASSPATH) you can do
java -cp $HOME/Documents/java/homework/bin src.EulerMath
But src.EulerMath is the name of your class. Alternatively, you can package it in a jar file with some meta-information (basically a Main-Class) and run it with java -jar <jarfile.jar>
2 Comments
src/EulerMath.class is in /home/mike/Documents/java/homework/bin). Your question says you set it to /home/mike/Documents/java/homework/bin/src; note the difference.I've managed to find the class (Note: not run it yet, i got a new error but thats a different story). Hopefully this helps someone else
Somewhere along these steps it fixed itself:
- make sure CLASSPATH is correct
- install javac
- use javac on file
- reboot computer
should work now
EulerMathin a package (what is the first non-commented line ofEulerMath)? The package name is part of the class in Java. The directory structure is not meaningless.