import java.io.*;
public class chk
{
String className;
String command,command1,command2;
public String getMsg(String fileName,String Path)
{
String dir;
command1="cd "+Path;
dir=Path.charAt(0)+Path.charAt(1)+"";
command2=dir;
command = "javac " + fileName;
String a=executeCommand(command1);
a=executeCommand(command2);
String output = executeCommand(command);
if(output.compareTo("")==0)
output = "Compilation Successfull!!";
return output;
}
private String executeCommand(String command)
{
StringBuffer output = new StringBuffer();
Process p;
try
{
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader1.readLine())!= null)
{
output.append(line + "\n");
}
while ((line = reader2.readLine())!= null)
{
output.append(line + "\n");
}
} catch (Exception e)
{
e.printStackTrace();
}
return output.toString();
}
public static void main(String args[])throws IOException
{
String x;
chk ob=new chk();
x=ob.getMsg("MyClass.java","D:\test");
System.out.println("OUtput : "+x);
}
}
我試圖運行在命令提示符下一堆命令與Java文件的幫助,使我可以在以後編譯另一個Java文件命名爲「MyClass.java」,它出現在我的電腦的其他驅動器,但我得到以下錯誤,說明它甚至不能執行我的第一個命令,即「command1 =」cd「+路徑;」這條線。請幫忙!
我會給出第三個參數嗎? – rick
我有一個路徑,即「D:\ test」如何將其轉換爲文件? – rick
'新文件(「D:\\ test」)'。這是在'File'類的文檔中。 – ajb