0

i need a java program in java that compiles other java programs using cmd commands

user496789
  • 107
  • 1
  • 4
  • 13

7 Answers7

5
Runtime.exec( -whatever cmd command you need to execute- )

http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html

Vinod.

user85421
  • 28,957
  • 10
  • 64
  • 87
Rajan
  • 626
  • 3
  • 8
  • 18
  • For JDK 1.5 and up, you can use `ProcessBuilder` (http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html) – Buhake Sindi Nov 08 '10 at 10:13
  • 1
    I edited your answer since there is no `RunTime` in Java, it's lowercase T. Note: I don't think that `exec` can run a cmd command directly, you need to run `cmd.exe`... – user85421 Nov 08 '10 at 10:38
  • oops thanks for pointing out the case mismatch Carlos.. bt i guess we shall be able tu run any cmd commands basically shell commands rite.. – Rajan Nov 08 '10 at 13:06
2

Maybe you are looking for Java Runtime.exec() function:

exec
public Process exec(String command)
              throws IOException

Executes the specified string command in a separate process. This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
obaqueiro
  • 982
  • 12
  • 29
1

To execute real cmd commands you need to start cmd.exe with the /c option using Runtime.exec or a ProcessBuilder like

    String cmd = "dir > t.txt";
    ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", cmd);
    Process process = builder.start();
    process.waitFor();
    System.out.println("done");


To start an executable like calc.exe you can start it directly

    ProcessBuilder builder = new ProcessBuilder("calc.exe");
    Process process = builder.start();
    process.waitFor();
    System.out.println("done");

both code samples missing IO and Exception handling...

user85421
  • 28,957
  • 10
  • 64
  • 87
1

Additional note:

If using JDK1.6 you can now programmatically compile from another java program using JavaCompiler. You could invoke your compiler program from the command line if this is what you are trying to achieve.

emt14
  • 4,846
  • 7
  • 37
  • 58
0

Not sure why you'd want to explicitly invoke the shell in order to compile Java programs. if you're absolutely sure that this is what you need to do, then go for it and follow the suggestions given by the others. However, if all you want to do is to compile Java code from within a Java program, you can do that with Java 6.0 (and up):

http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html

Isaac
  • 16,458
  • 5
  • 57
  • 81
0

using the cmd could be done like this:

String cmd = "c:\\Programme\\Ghostgum\\gsview\\gsprint.exe"; //what to execute
String prt = "-printer XYZ"; // additional parameters
String dat = "\"" + pfad + "\""; // the file to be passed
ProcessBuilder pb = new ProcessBuilder(cmd, prt, dat);
System.out.println(cmd + " " + prt + " " + dat);
pb.redirectErrorStream(true);
Process ps = pb.start();
ps.waitFor();
oezi
  • 51,017
  • 10
  • 98
  • 115
  • 1
    Note 1: not running `cmd.exe`, the `shell` variable is not being used at all. 2: `gsprint.exe` is not a cmd command. 3: there is no reason to call `redirectErrorStream` if you are not reading the standard output (not wrong anyway). – user85421 Nov 08 '10 at 10:48
0

I finally got my answer. It actually compiles a Java program. The program is as follows:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Dos {
    public static void main(String[] args) {
        try {
            String[] command = new String[4];
            command[0] = "cmd";
            command[1] = "/C";
            command[2] = "C:/Program Files/Java/jdk1.6.0_21/bin/javac";//path of the compiler
            command[3] = "d:\\a.java";

            Process p = Runtime.getRuntime().exec(command);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command

            String s = null;
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // read any errors from the attempted command

            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
            System.out.println("I am In try");
        }

        catch (Exception e) {
            System.out.println("I am In catch");
        }
    }
}
brimborium
  • 9,362
  • 9
  • 48
  • 76
user496789
  • 107
  • 1
  • 4
  • 13
  • As a compiler, I would refuse to compile this mess. Please use proper code formatting (preferably the [Java Code Conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html)). :D – brimborium Oct 09 '12 at 08:35