Rainie Rainie - 1 year ago 139
Java Question

running java main class using subprocess.Popen in python

I want to execute java main class

by python using
takes 3 args.

I wonder how to do it? For example I have a

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!" + args[0]);
} }

I tried to call it in python using the command:

print (subprocess.Popen('java C:/Users/testing/Hello.Main sayHello',
shell=True, stdout=subprocess.PIPE).stdout.read())

where 'sayHello' is the string args I want to pass in. It said

Error: Could not find or load main class C:.Users.testing.Hello.Main


Answer Source

You may run your java file with extension .class the following way:

java your.path.Main arg1 arg2


  • java - command, which runs the Java interpreter
  • your.path.Main - full name of your class (without .class)
  • arg1 arg2 - the arguments (written by spaces or between ")

Further, when you formatted this line, it transmits in subprocess.Popen() as argument.

subprocess.Popen('java your.path.Main arg1 arg2')

I'm not Python programmer, because I advice you to read documentation about this method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download