user1758401
user1758401

Reputation: 29

Unable to run a java program from another java program

Iam trying to run a java file(editor.java) from another java program(exec.java). It takes input and displays file not found message.please give me suggestion through which I run a progam succesfully.

    import java.io.*;
    public class exec {

    public static void main(String argv[]) {
    try {
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(isr);

    System.out.println("Enter the java class name");
    String s=br.readLine();
    String[] cmd = {"java", "-cp", "E:\netbeans\Project\src", s};
    Process pro=Runtime.getRuntime().exec(s);
    try (BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()))) {
        String line=null;
        while((line=in.readLine())!=null) {
            System.out.println(line);
        }
        }
        } catch(Exception err) {
    err.printStackTrace();
   }
 }


java.io.IOException: Cannot run program "editor.java": CreateProcess error=2, The system   cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at project.exec.main(exec.java:18)

Upvotes: 0

Views: 927

Answers (4)

Praveenkumar_V
Praveenkumar_V

Reputation: 1394

inside E:\netbeans\Project\src you found only source file, source file u could not run,,,

try to do in class file found inside *E:\netbeans\project\build\classes*

String[] cmd = {"class", "-cp", "E:\netbeans\project\build\classes\", s};

NOTE: Check your class path

Thank you

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213371

You need to pass your .class file in your command line argument. You don't run a .java file with a java command.

Just pass editor as argument, if the class containing your main method is editor.class.

Also, do follow the @Azodious's answer below.

Also, you might need to change the path in your array to the path containing the class file. src folder might not be having your class file

So, run your program using: - java exec editor. I think that should work.

Upvotes: 3

devang
devang

Reputation: 5516

Multiple issues -

  1. You are trying to run command "editor.java" from command line. Your command array remains unused.
  2. Are your compiled classes in the same directory as source? Usually, with eclipse the classes are generated in bin folder. You should be doing -

    String[] cmd = {"java", "-cp", "E:\netbeans\Project\bin", s};
    Process pro=Runtime.getRuntime().exec(s);

  3. Is your editor.java in the default package? If not, you need to enter fully qualified name when running the command.

I would suggest try running the class from command line, and then form the same command from Java code.

Upvotes: 0

Azodious
Azodious

Reputation: 13882

You are not passing commands array to exec method

Change it to following:

Process pro=Runtime.getRuntime().exec(cmd);

and, your error shows that you are trying to run src file:

Cannot run program "editor.java"

You should pass the .class file name to run it.

Upvotes: 1

Related Questions