2

I want to make a program that open another java programs.how can i run/execute the cmd command in compiling and running java programs.

for example c:\Users\Burnok> javac HelloWorld.java and c:\Users\Burnok> java HelloWorld

how can i do that inside the java program? please help.

I tried this code but it compiled successfully but if i tried to run the HelloWorld.class it says that the could not find or load main class.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;


public class Test {

     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();
      }

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

}

here is the error java src/HelloWorld stderr: Error: Could not find or load main class src.HelloWorld

5
  • Check this stackoverflow.com/questions/4842684/… Commented Aug 1, 2014 at 13:50
  • i try that but if i tried to run the .class it says that could not find or load main class. Commented Aug 1, 2014 at 13:53
  • can you post exactly what you have tried? Commented Aug 1, 2014 at 13:55
  • see mkyong.com/java/how-to-execute-shell-command-from-java Commented Aug 1, 2014 at 14:02
  • Have you checked where HelloWorld.class gets generated and where the current context is when you execute? Is is possible the class file is inside the src directory? Commented Aug 1, 2014 at 14:07

1 Answer 1

1

Your are supposed to mention class path while running from another directory

syntax is java -classpath directory_to_program Program

try {
        runProcess("javac src/HelloWorld.java");
        runProcess("java -classpath src HelloWorld");
    } catch (Exception e) {
      e.printStackTrace();
    }

Read for more info How do I run a java program from a different directory?

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks it display hello world but i want to run just like the Test class that open another cmd
@user3276091 sorry can you explain 'that open another cmd' ?
@user3276091 sujithvm just fixed the bug, not introduced the new way to run, what's the problem then?
yeah but sorry but i want to open another cmd that display helloWorld
@user3276091 Do you another window with command line to be opened?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.