tenkii
tenkii

Reputation: 449

How to run a java class as a process

Part of my program will need to execute another java program I wrote as a process, if I write:

       Runtime runtime = Runtime.getRuntime();
       Process process = runtime.exec(<path to class file>);

Why does this not work? The error tells me its not a vlaid win32 application.

Upvotes: 0

Views: 368

Answers (3)

Chris Mantle
Chris Mantle

Reputation: 6683

Well, it's right: a class file is not a valid Win32 application. You need to execute the class file with the Java runtime:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("java [YourClassName]");

This assumes that you have a main method in your class.

Upvotes: 1

Edi G.
Edi G.

Reputation: 2422

(<path to class file>) ... to the jar? or to the myClass.class?

can you run the class in a console:

java -jar myClass.jar

Upvotes: 0

arcy
arcy

Reputation: 13123

Well, it doesn't work because it is NOT a win32 application.

Class files are run by the java virtual machine, not by the Windows operating system. You could try feeding the command 'java -jar .jar, once you've packaged your class file and the other class files that it needs in a jar file.

Upvotes: 0

Related Questions