1

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

asked Feb 17, 2016 at 4:48
3
  • Have you compiled the .java file? Commented Feb 17, 2016 at 4:50
  • Is EulerMath in a package (what is the first non-commented line of EulerMath)? The package name is part of the class in Java. The directory structure is not meaningless. Commented Feb 17, 2016 at 4:58
  • @MadProgrammer yes, using both javac command and eclipse ElliotFrisch yes, as stated above it is in the src package and that is included in my commands Commented Feb 17, 2016 at 5:00

2 Answers 2

0

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>

answered Feb 17, 2016 at 5:03
Sign up to request clarification or add additional context in comments.

2 Comments

i have already modified CLASSPATH in ~/.bashrc, where i modified my PATH as well. The CLASSPATH var reflects this change.
@mike Note that the CLASSPATH I have provided here matches where you said your classes are (namely 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.
0

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

answered Feb 17, 2016 at 5:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.