i need a java program in java that compiles other java programs using cmd commands
Runtime.exec( -whatever cmd command you need to execute- )
http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html
Vinod.
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).
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...
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.
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();
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
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");
}
}
}
精彩评论