0

I'm running OSX Yosemite with jdk1.8.0_31. While I was testing printf functionality, I realized that I can't run any .class files in terminal with command: java MyClass. Here's the error I get:

Error: Could not find or load main class MyClass.class

Also I can run classes with main method perfectly fine in Eclipse, and I can compile MyClass.java with java compiler in terminal by $javac MyClass.java.

I googled it and asked around, still couldn't find anything.

Any ideas?

  • Is `MainExp` in a package? This problem usually occurs because you are not running the class from within the correct context. You should make sure that you are at the root directory of the package of the class, then use the fully qualified name of the class to execute it `java some.package.MainExp` for example... – MadProgrammer Mar 19 '15 at 03:07
  • @SebastianO. How many classes do you have? Are `MainExp` and `MyClass` two separate classes? – iRuth Mar 19 '15 at 03:34
  • @MadProgrammer: Yes it is. It's in package `com.foo.root`. I ran both `java MainPackage.bin.com.foo.root.MainExp` and `java MainExp` in all directories starting from `root` to all the way to outside `MainPackage` project folder. Same error. – Sebastian O. Mar 20 '15 at 04:48
  • @iRuth: There's just one class, `MainExp`. – Sebastian O. Mar 20 '15 at 04:54
  • @All: For clarity, I changed the description to have one class (`MyClass`). – Sebastian O. Mar 20 '15 at 15:07
  • What does your class path setup look like? – Mark Mar 20 '15 at 16:09
  • @Mark: My `$CLASSPATH` is not set. Within the directory that has `MyClass.java`, I can run `javac MyClass.java` and it runs & creates the `MyClass.class` in the same / current directory. Then I tried both with `java -cp ./ MyClass` and `java MyClass` and same error. I also tried running with `java -cp /Users/someuser/Documents/workspace/MainPackage/src/com/foo/root`, also same error. – Sebastian O. Mar 21 '15 at 15:41
  • Have you attempted to the class path without the package specifier? IE: `-cp /Users/someuser/Documents/workspace/MainPackage/src`? – Mark Mar 22 '15 at 03:59
  • @Mark: Yup. Tried on all directories recursively with `-cp /Users/someuser/Documents/workspace/MainPackage/src` and still the same error. This error is going to drive me crazy. I can't find anything, anywhere. – Sebastian O. Mar 23 '15 at 05:21
  • Can you provide full command you are using to start java with the above class path? – Mark Mar 23 '15 at 19:29
  • @Mark: While I am within starting from `/Users/someuser/Documents/workspace/MainPackage/src/com/foo/root` directory to all the way up to `/Users/someuser/Documents/workspace` recursively, I ran this same command `java -cp /Users/someuser/Documents/workspace/MainPackage/src MyClass`. Please note that I'm using `zsh` and `zsh` is capable of tab completion, and when I do it, it finds the `MyClass` compiled `.class` file... So I don't think it's a visibility problem. – Sebastian O. Mar 23 '15 at 20:01
  • Doh! When running main classes they should be fully qualified class name. What error does the following give you? `java -cp /Users/someuser/Documents/workspace/MainPackage/src com.foo.root.MyClass` – Mark Mar 23 '15 at 21:41

0 Answers0