Home » Java » Error: Could not find or load main class java-example.HelloWorld.java

Error: Could not find or load main class java-example.HelloWorld.java

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am trying to execute a JAVA program through Python and I am using the subprocess module. I tried to run the following code:

subprocess.Popen(["java", "java-example/HelloWorld.java"])

And it runs okay, didn’t return any output.

Next, I tried to run is this:

subprocess.call(["java", "java-example/HelloWorld.java"])

It gave me an error of:

import subprocess

Error: Could not find or load main class java-example.HelloWorld.java

My source code in Python looks like this:

import subprocess

subprocess.call(["java", "java-example/HelloWorld.java"])

And the JAVA file source code is:

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello world!");
    }

}
Answers: