If I have eclipse-workspace/bin/test/example.class in my mac
I try java example
(in test folder, use mac terminal)
But it doesn't work just say
Error: Could not find or load main class haha
But it works java test/example
(in bin folder)
How can i declare just 'java example' in test folder?
(I probably think it's about PATH, but i can't find solution...)
CodePudding user response:
The argument to the java
executable is not a file. It's a fully qualified class name. To run such a name, java needs to know where the class file lives (as well as the class files for any types mentioned in it, of course) - and it does this by searching the classpath (not the PATH
), which are file system paths - you can specify either a jar file, or a directory. Either way, the class files need to exactly match the full names, e.g. the full name of the String class is java.lang.String
. It's the package the name (String.java
starts with package java.lang;
).
It's not clear whether you have told eclipse that bin/test
is the base folder, or if bin
is the base folder and your example class has package test;
at the top.
You'd run something like this:
java -cp ~/eclipse-workspace/bin/test example
# or - depends on that package stuff
java -cp ~/eclipse-workspace/bin test.example
It gets more complicated if you have deps involved. Then it might look like:
java -cp ~/eclipse-workspace/bin:'~/eclipse-workspace/lib/*' test.example
Note the single quotes: You do NOT want bash (your shell) to expand that star. It also depends on your eclipse config. Your question cannot possibly be an eclipse config, or at least, not without some wildly odd setting shenanigans - normally it's workspace/projectname/bin/fully/qualified/package/YourClass.class
- i.e. workspace/projectname/bin
is the classpath. Not workspace/bin
.