1

I have 2 classes one is a simple one

Sample.java

public class Sample {
  public static void main(String args[]) {

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

Other one is something like this

Main.java

public class Main
{  
  public static void main(String[] args) throws Exception
  {
     Runtime.getRuntime().exec("java Sample");
  }
}

I am basically trying to run the Main.java program to call Sample.java in a new command prompt...that is a new cmd that should open and print the output of Sample.java...how should I do this...???

6
  • 1
    And what is the outcome if you execute Main? Commented Sep 13, 2013 at 7:45
  • what you got in your result? Commented Sep 13, 2013 at 7:45
  • 1
    I think it is laready answered, you can check this - How to open the command prompt and insert commands using Java? Commented Sep 13, 2013 at 7:46
  • nothing it just shows blank.... Commented Sep 13, 2013 at 7:46
  • Are you asking this because you want to learn about exec ? Or do you just dont know how to invoke methods in another class? Commented Sep 13, 2013 at 8:00

4 Answers 4

4

Compile the two together, and then from Sample,

Main.main(args);

will do the trick. You don't need to import since you're in the same package. Note the linked tutorial. http://docs.oracle.com/javase/tutorial/java/package/index.html

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

2 Comments

@RahulMehrotra Can you accept this answer if it works for you?
bro ..this is not what I am exactly looking for but heads up for the help....
2
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"cd <where_the_Sample_is> && javac Sample.java && java Sample\"");

or if the class is already compiled:

Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"cd <where_the_Sample_is> && java Sample\"");

4 Comments

this work but the problem here is that it shows the ouput for fraction of a second and then closes..any idea as to how retain the new commandpromt..??
try adding " && pause" at the end of the command, for example: "cmd /c start cmd.exe /K \"cd <where_the_Sample_is> && java Sample && pause\"
doesnt work...it still exists
okay I was able to make it halt by using getInt();
2

I am using eclipse. The class files are placed in the bin directory located under the projects directory. The below code starts command prompt, changes directory to bin and issues java Sample command. You can edit it up to your requirement.

Runtime.getRuntime().exec("cmd.exe /c cd \"bin\" & start cmd.exe /k \"java Sample\"");

Comments

0

You can use this code:

public class Main {
    public static void main(String[] args) throws Exception {
        Class<Sample> clazz = Sample.class;
        Method mainMethod = clazz.getMethod("main", String[].class);
        String[] params = null;
        mainMethod.invoke(null, (Object) params);
    }
}

7 Comments

I actually need to start a new cmd prompt and show the output there...thanks a lot for your help...:)
Go to the directory where is Main.class is put and use java command: java Main
Make sure that Main.class and Sample.class are in the same directory, otherwise you must use -classpath option of the "java" command line
it is showing an error as cannot find the class Method
Can you copy and past here the error ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.