gpt4 book ai didi

java - 无法通过运行时类编译构建.class文件

转载 作者:行者123 更新时间:2023-12-01 15:10:10 25 4
gpt4 key购买 nike

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),但无法构建类文件。

            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();
}
}

最佳答案

如果您将类名保留在 sout 中(如 input 文本字段中提供的那样),那么您的输出编译命令应为:

Process p=rt.exec("javac " + sout + ".java", null, new File("C:/c/compile assign"));

您的运行命令应该是:

Runtime.getRuntime().exec("java " + input.getText(), null, new File("C:/c/compile assign")).getInputStream();

在启动 java 命令之前,您可能需要在使用此字段中的原始值(例如类存在)之前添加进一步的验证。

关于java - 无法通过运行时类编译构建.class文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12437111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com