2

Hi guys I want to run java programs inside my javaprogram.

However when I tried to execute java command it tells me this:

java c:\works\Sample stderr: Error: Could not find or load main class c:\works\Sample

This is my code:

private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        Processor p = new Processor();
        try {
            int k = p.runProcess("javac c:\\works\\Sample.java");
            if (k == 0) {       
                k = p.runProcess("java c:\\works\\Sample");
            }

            System.out.println("Value of k: " + k);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }      

This is my Processor Class

public class Processor {

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

public int 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());
    return pro.exitValue();
}

}

My files were under "C:\works\"

Your responses would be greatly appreciated!

iamjc015
  • 2,127
  • 6
  • 25
  • 61
  • `java` is different than `javac`. One of basic differences it that it expects `full.package.name.of.YourClass`, not `c:/location/of/YourClass`. Also it expects in its classpatch parameter location of package with class you want to run. – Pshemo May 31 '15 at 09:15
  • What If i have no package? – iamjc015 May 31 '15 at 09:16
  • under works folder were the .class file and .java file. Only two files there. – iamjc015 May 31 '15 at 09:17
  • use `java -cp locationODirectoryHoldingfYourClassWithoutPackage YourClass` – Pshemo May 31 '15 at 09:17
  • ^that worked! but it writes on my console only. What IF i want it to appear on CMD itself? – iamjc015 May 31 '15 at 09:18
  • I am not sure what you mean by "my console only" and "on CMD itself". – Pshemo May 31 '15 at 09:19
  • Ok, I run those codes in an IDE. And the output was in System.out.println(). What If I clicked the button the CMD showed up then theres the output? – iamjc015 May 31 '15 at 09:21
  • I am still unsure what you mean. Also "And the output was in System.out.println()." doesn't seem right because `println` simply passes data to print, it doesn't decide where to print it (by default it is console from which our command was started/run). – Pshemo May 31 '15 at 09:32
  • Why not call the class directly? – Tassos Bassoukos May 31 '15 at 09:56

1 Answers1

1

You can do this by having one master program which you run from your CMD which runs whatever processes you want and redirect input/output of these processes to its own input/output which happens to be your CMD input/output. Here is good example.

Community
  • 1
  • 1
Izold Tytykalo
  • 719
  • 1
  • 5
  • 15