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