import java.io.*;
public class B
{
public static void main(String args[])
{
try{
Runtime.getRuntime().exec("cmd.exe /c javac C:\\Documents and Settings\\Administrator\\桌面\temp\\A.java");}catch(IOException e1){}
try{Thread.sleep(5000);}catch(InterruptedException e){}
try{Runtime.getRuntime().exec("cmd.exe /c java C:\\Documents and Settings\\Administrator\\桌面\\temp\\A");}catch(IOException e2){};
System.out.println("111");
}
}
A.java手动可以正常编译后运行,为什么将B编译运行后,对A.java没有作用,既不能编译也不能运行,环境变量设置正确
public class B
{
public static void main(String args[])
{
try{
Runtime.getRuntime().exec("cmd.exe /c javac C:\\Documents and Settings\\Administrator\\桌面\temp\\A.java");}catch(IOException e1){}
try{Thread.sleep(5000);}catch(InterruptedException e){}
try{Runtime.getRuntime().exec("cmd.exe /c java C:\\Documents and Settings\\Administrator\\桌面\\temp\\A");}catch(IOException e2){};
System.out.println("111");
}
}
A.java手动可以正常编译后运行,为什么将B编译运行后,对A.java没有作用,既不能编译也不能运行,环境变量设置正确
Runtime.getRuntime().exec("cmd.exe /c javac \"C:\\Documents and Settings\\Administrator\\桌面\temp\\A.java\"");
目录里有空格,加个"试试看。(如果环境变量真的正确的话,比如javac所在的bin目录在path环境变量里了?)
Runtime#exec(String[] cmdarray)
或
Runtime#exec(String[] cmdarray, String[] envp)
具体请参考API Doc
Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "javac", "C:\\Documents and Settings\\Administrator\\桌面\temp\\A.java"});