0

I have a simple program that works fine in a previous environment. I've moved it to a new one with the same OS and Java versions, but have encountered a problem executing the class files.

Never have I had a problem running a java class in the same directory but when I try java Class I get the error could not find or load main class.

I've already looked over all the suggestions, one of the most popular being to add the classpath and run it as java -cp . Class. However this has never been necessary in all the environments I've worked in, including the previous one which was identical to this. Why would this happen and how can I fix it?

Note: In case you wonder, java -cp . Class works for a small test program with no dependencies, but results in a JNI error which has no clear resolution, related to a jar file that is both on the bashrc classpath and the current directory (when specifying -cp .). Regardless, I just want to execute classes like normal.

UPDATE

I'm certain this is an environment issue, but here is the code. I am calling it as java Test, NOT java test or java Test.class.

class Test {
    public static void main(String[] args) {
        System.out.println("Test complete!");
    }
}
3
  • In your previous environment, did you have a CLASSPATH environment variable set? – glenn jackman Jan 1 '19 at 1:41
  • @glennjackman Yes. And it is the same as in the new one. I had added an entire directory to the path and it was failing. By specifying the actual class name in CLASSPATH it seems to work. But this still wasn't necessary before... and it's strange that other jars in the directory I specified were recognized but not the .class file. – user58446 Jan 1 '19 at 2:00
  • You might want to peruse some of the Related questions you'll find displayed in the right-hand column. – glenn jackman Jan 1 '19 at 3:17
0

You need to compile your Java source code with javac Test.java which makes a new file named Test.class before you run it. Otherwise you will get this error message: Error: Could not find or load main class Test as shown below. If you run these commands one at a time, it will show you which command gives unexpected results.

$ cat Test.java
class Test {
    public static void main(String[] args) {
        System.out.println("Test complete!");
    }
}
$ java Test
Error: Could not find or load main class Test
$ javac Test.java  
$ ls
Test.class  Test.java
$ java Test
Test complete!
$ java -cp ~/Desktop/ Test # test using classpath
Test complete!

As you can see, the last command has the directory containing the Test executable file after the -cp classpath option, not the Test file itself as you wrote in your question.

2
  • This was not the issue, I was able to compile the program successfully before running the class. What I still haven't figured out is why in the past I could create a .java file in any random directory, compile it (from that directory) and run the class (from that directory) without modifying the classpath. This is while running the class from the current directory, as I wouldn't expect it to find it in another directory by itself. – user58446 Jan 1 '19 at 6:50
  • I added another test to the list of tests in my answers, this test using classpath. Try copying Test.class on your Desktop first, so you can easily see what files are created by the commands. – karel Jan 1 '19 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.