2

I tried running the accepted answer in question using netbeans. The class file created but it is not executing. Here is my code

import java.io.*;

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 /home/shibin/NetBeansProjects/JavaApplication3/src/javaapplication3/Main.java");
      Thread.sleep(1000);
      runProcess("java /home/shibin/NetBeansProjects/JavaApplication3/src/javaapplication3/Main");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Main.java is just a print statement,

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

and i am getting error

javac /home/shibin/NetBeansProjects/JavaApplication3/src/javaapplication3/Main.java exitValue() 0

java /home/shibin/NetBeansProjects/JavaApplication3/src/javaapplication3/Main stderr: Error: Could not find or load main class 
.home.shibin.NetBeansProjects.JavaApplication3.src.javaapplication3.Main

java /home/shibin/NetBeansProjects/JavaApplication3/src/javaapplication3/Main exitValue() 1
Community
  • 1
  • 1
  • Also paste your Main.java – Naved Alam Mar 17 '14 at 16:25
  • have you tested those command lines before using them in your code? The problem is there: the executer is looking for the package home.shibin.NetBeansProjects.JavaApplication3.src.javaapplication3 (classpath issue) – Pablo Lozano Mar 17 '14 at 16:38
  • This code works perfectly fine while executing using terminal. When using netBeans, Class file is created in the folder home.shibin.NetBeansProjects.JavaApplication3.src.javaapplication3 but not executing. – Neethu Krishna Mar 17 '14 at 16:55

3 Answers3

0

The command for running java program is java packageName.programName or java packageName/programName The problem here is that you are running the code with java pathOfProgram/programname. To resolve this you have to supply classpath using -cp argument followed by the programPath like shown below:

java -cp pathOfProgram packageName.programName
Sourabh Bhat
  • 1,793
  • 16
  • 21
  • to add to above comment, your command should be [[ java -cp /home/shibin/NetBeansProjects/JavaApplication3/src/javaapplication3 Main ]] I see no package statement, so it is in default package. Also, just check out this link - http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html It maybe a bit advanced in case you are a beginner, but you will find some good practices there. – RuntimeException Mar 18 '14 at 08:56
0

try this:

runProcess("java -cp /home/shibin/NetBeansProjects/JavaApplication3/src/javaapplication3 Main");
gsamaras
  • 71,951
  • 46
  • 188
  • 305
sara
  • 1
  • 1
-1

Your java command should have classpath argument. Prefer to use cmd array. Java process api is not xommand line.

RuntimeException
  • 1,593
  • 2
  • 22
  • 31