0

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)
3
  • Are you passing editor.java as commadline argument? Commented Oct 30, 2012 at 6:03
  • yes iam passing editor.java as a Commandline argument Commented Oct 30, 2012 at 6:07
  • You need to pass a class file. You can't run a .java file with java command right? Commented Oct 30, 2012 at 6:09

4 Answers 4

3

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.

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

2 Comments

@user1758401. First of all check where is your class file that you want to run. Then copy the path till the folder containing that class file in your code, in the cmd array you have made. Now, run your above code using the command: - java exec editor. Where I assume that editor is the name of your class containing the main method. You can change that name with the corresponding class file name.
@user1758401. Also, check that your classpath contains the path till the folder containing your class file.
1

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.

1 Comment

try using only: E:\netbeans\Project
0

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.

Comments

0

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

1 Comment

java.lang.RuntimeException: Uncompilable source code - illegal start of expression at project.exec.main

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.