我想在我的JFrame GUI中運行一個基於Linux的可執行文件src2srcml,它採用源文件(C,C++,Java)並將其轉換爲要使用的XML文件後來。我的GUI成功使用JFileChooser來定位和選擇源文件。從JFrame(linux)(日食)運行可執行文件
我目前正在嘗試使用Runtime.getRuntime().exec()函數來運行可執行文件,但到目前爲止沒有任何事情發生。我可以使用命令「bash-4.1 $ ./src2srcml --position sum_avg.c -o hooray.c.xml」從命令行運行它,它將源文件sum_avg.c轉換爲新的XML文件文件hooray.c.xml,但是當我在Runtime.getRuntime().exe()中嘗試完全相同的命令時,沒有任何反應。我對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嵌入到我的可執行文件中,而不需要單獨執行。這可能嗎?
在此先感謝您的任何想法!