【发布时间】:2014-11-07 03:39:37
【问题描述】:
我正在尝试在我的 JFrame GUI 中运行一个名为 src2srcml 的基于 linux 的可执行文件,它获取源文件(C、C++、Java)并将其转换为 XML 文件以供以后使用。我的 GUI 成功地使用 JFileChooser 来定位和选择源文件。
我目前正在尝试使用 Runtime.getRuntime().exec() 函数来运行可执行文件,但到目前为止还没有发生任何事情。我可以使用命令“bash-4.1$ ./src2srcml --position sum_avg.c -o hooray.c.xml”从命令行运行它,该命令获取源文件 sum_avg.c,将其转换为新的 XML文件 hooray.c.xml 但是当我在 Runtime.getRuntime().exec() 中尝试完全相同的命令时,没有任何反应。我对 Runtime.getRuntime().exec() 或 ProcessBuilder 类不是特别熟悉。我需要导航到可执行文件的第一个位置吗?我还尝试在第一个参数中包含路径,对可执行文件自身的调用,但这也不起作用。
//--- 'Run Source Code' Button---//
JButton btnRunSourceCode = new JButton("Run Source Code");
btnRunSourceCode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(filePath == null){
textArea.setText("Please Load a source file (C, C++, Java)");
}
try{
textArea.setText("Converting Source Code to XML");
String[] commands = {"./src2srcml", "--position ", selectedFile.getName(), " -o targetFile.xml"};
Process src2XML = Runtime.getRuntime().exec(commands);
src2XML.waitFor();
}
catch(Exception exc){/*src2srcml Fail*/}
}
});
btnRunSourceCode.setBounds(612, 39, 156, 25);
contentPane.add(btnRunSourceCode);
当前可执行文件位于我的项目 (Eclipse) 的工作区中。当我一切正常时,我想将整个程序编译成一个可执行文件,以便将 src2srcml 嵌入到我的可执行文件中,而不需要单独使用。这可能吗?
提前感谢您的任何想法!
【问题讨论】:
标签: java linux eclipse swing executable