Running a Java Program from Another Java Program

Running a java program from another java program

It is a bit strange but you can run the second program without forking it. Just calling the main method in it. So forget the runtime section and do this:

sam2.main(new String[0]);

Of course this way you must compile sam2 at compile time

Compile and run a Java program from another Java program

The Problem here is you are passing argument

./Main.java

instead, you should pass Main.java as an argument else you need to change your getProgramName() method to return the Class name correctly.

Which will let you compile the program perfectly with javac command but problem happens when you need to run the program because that command should be

java Main

whereas you are trying to execute

java ./Main

how to compile & run java program in another java program?

I have modified the code to include some checks:

public class Laj {

private static void printLines(String name, InputStream ins) throws Exception {
String line = null;
BufferedReader in = new BufferedReader(
new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(name + " " + line);
}
}

private static void runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
pro.waitFor();
System.out.println(command + " exitValue() " + pro.exitValue());
}

public static void main(String[] args) {
try {
runProcess("javac Main.java");
runProcess("java Main");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Here is the Main.java:

public class Main {
public static void main(String[] args) {
System.out.println("ok");
}
}

When everything is fine, it just works:

alqualos@ubuntu:~/tmp$ java Laj
javac Main.java exitValue() 0
java Main stdout: ok
java Main exitValue() 0

Now, for example, if I have some error in Main.java:

alqualos@ubuntu:~/tmp$ java Laj
javac Main.java stderr: Main.java:3: package Systems does not exist
javac Main.java stderr: Systems.out.println("ok");
javac Main.java stderr: ^
javac Main.java stderr: 1 error
javac Main.java exitValue() 1
java Main stdout: ok
java Main exitValue() 0

It still prints "ok" because the previously compiled Main.class is still there, but at least you can see what exactly is happening when your processes are running.

CodeCompiler - Compile and Run Java Program from another Java Program

Since Java 1.6, Java has had a programmatic API to access the Java compiler without using a subprocess. As a starting point, see the documentation for the JavaCompiler class.

Running a java program located at any location from another java program

Try something like: java -classpath . Hello.
If you didn't specified package you must run Hello.class from the same directory.

runProcess(cd H:/Study/eclipse_workspace/advance/src/)

And then

runProcess(java Hello)

How to compile and run MULTIPLE java programs from another java program without knowing their class names?

Normally to run any program, you need to know the name of the program that you will execute.
Now, if "all" the classes on your zip file will be run, then you can:

  1. Unzip the files.
  2. Read all the files names (classes) and put
    them in a String[].
  3. Parse the String[] and execute each name
    (class) on the array as a class; you may need Reflection enter link
    description here.

Let me know if you need more help.

Try something like this:

    File directory = new File("c:\temp");
File[] files = directory.listFiles();
for (File thisClass : files) {
Runtime runTime = Runtime.getRuntime();
try {
Process process = runTime.exec(thisClass.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


Related Topics



Leave a reply



Submit