0
public void actionPerformed(ActionEvent e)
{
String sout;
try
{
if(e.getSource()==compile)
{
sout=input.getText();
Runtime rt=Runtime.getRuntime();
Process p=rt.exec("javac sout.java",null,new File("C:/c/compile assign"));
}
我在String sout(通過TextField)中存儲類名,然後調用sout.java(即classname.java),但無法構建類文件。無法通過運行時編譯生成.class文件
if(e.getSource()==run)
{
InputStream is = Runtime.getRuntime().exec("java input.getText()",null,new File("C:/c/compile assign")).getInputStream();
//BufferedInputStream b=new BufferedInputStream(is);
BufferedReader br= new BufferedReader(new InputStreamReader(is));
output.setText(br.readLine()+"hello");
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
對'exec'或更好的'ProcessBuilder'的命令最好保存在'String []'中。請務必訪問['exec' info鏈接的Java World文章。頁面(http://stackoverflow.com/tags/runtime.exec/info)。實施所有建議。 –