1

I just upgraded to the latest Java

> java -version
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02, mixed mode)
> javac -version
javac 1.7.0_03

I am having issues to execute Java program from the command-line. For instance:

public class Tester {
    public static void main(String[] args) {
        System.out.println("in main");
    }
}

I compiled it on the command-line, then try to execute it:

> javac Tester.java
> java Tester
Error: Could not find or load main class Tester

Is this a bug? Strangely, I have no problem to execute the program using Eclipse.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Mr.
  • 9,429
  • 13
  • 58
  • 82
  • Is the `Tester` class in the default package? And is the file `Tester.class` in the current directory after compiling? – millimoose Feb 18 '12 at 18:39
  • @Inerdial, the _Tester_ class is exactly as shown here, with no package reference. and yes, the _Tester.class_ file resides in the same directory of executing the compilation and the _java_ iteself. – Mr. Feb 18 '12 at 18:41
  • 2
    Had to test, exact same program works for me with same version. Only thing I can think of checking is that the classpath includes `.` – Joachim Isaksson Feb 18 '12 at 18:50
  • @JoachimIsaksson, Indeed. And the conclusion is -- do not code late in night. THANKS! any clue how do I mark this thread as closed\solved? – Mr. Feb 18 '12 at 18:58
  • I think you should be able to mark the answer from @gsteff below as a solution, the comments up here can't be marked as that :) – Joachim Isaksson Feb 18 '12 at 19:05
  • 1
    This is why you should never ever set the `CLASSPATH` environment variable, certainly not globally. – millimoose Feb 18 '12 at 22:26

5 Answers5

2

Run jar files in console mode. java -jar filename.jar

venat0r888
  • 41
  • 3
2

Is your CLASSPATH environment variable set? When I do

export CLASSPATH=/tmp
java Tester

I get a NoClassDefFoundError, though not the exact same error message you quoted.

gsteff
  • 4,764
  • 1
  • 20
  • 17
  • the ``classpath`` has nothing to do with this issue. – Mr. Feb 18 '12 at 18:48
  • I on the other hand get the exact same error message with the same Java version on Windows when I don't have `.` in the classpath. – Joachim Isaksson Feb 18 '12 at 18:59
  • 2
    @MrRoth: It does. If you have set `CLASSPATH` and it does not include `.` or another reference that indirectly includes `.`, you will get that exact error. Try `set CLASSPATH=` in a console window and if it works then, that was your problem. (I know you use Windows since the build number is different on Linux/Solaris) – mihi Feb 18 '12 at 19:02
  • @mihi, thanks! how do I mark this thread as closed? can you help me with different thing? http://stackoverflow.com/questions/9340538/eclipse-terminate-shotcut – Mr. Feb 18 '12 at 19:03
  • the solution is with _mihi_ :) just add the current working directory to the classpath – Mr. Feb 18 '12 at 22:07
0

I had the same error when trying to run Tomcat and realized it was because I was using a 64 bit version of Tomcat on a 32 bit system. Once I tried the 32 bit version, it worked.

gomisha
  • 115
  • 1
  • 1
  • 7
0

if you want to run program in current working directory where your class reside.

java gives three options.

first option

java -cp Tester

Second option for current working directory

java -cp . Tester

Third option export CLASSPATH variable

export CLASSPATH=$CLASSPATH:. (this is the best one if your directory changes) or

export CLASSPATH=$PWD

or

export CLASSPATH=

after that you must sorce the bashrc or bashprofile.

user1651008
  • 27
  • 1
  • 8
0

just set your classpath in system variables:

classpath=.
laalto
  • 150,114
  • 66
  • 286
  • 303
Alex Gorbunov
  • 239
  • 3
  • 8