我使用基本 Java 创建了一个工具来帮助我更快地测试 Android 设备。我正在使用 Linux Ubuntu,尝试运行 .sh 脚本。我创建了一个带有一些按钮的小 JFrame,其中一个按钮打开 (JFileCoser) 一个文件夹,并且可以正常工作;但知道我希望能够选择一个脚本 .sh 文件并使其在单击“打开”按钮时运行。但我似乎无法让它发挥作用!这是我来自 actionclass 页面的代码。 请记住,我想让 .sh 脚本文件运行并能够在 .sh 脚本本身上运行命令。 非常感谢任何帮助!
代码:
import java.awt.event.ActionEvent
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class adbScript extends JPanel implements ActionListener {
private File runfile;
@Override
public void actionPerformed(ActionEvent arg0) {
{
//Handle open button action.
JFileChooser("/home/local/ANT/arthm/Desktop/stuff");
FileNameExtensionFilter filter = new FileNameExtensionFilter(".sh", "sh");
adbfile.setFileFilter(filter);
int returnVal = adbfile.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
runfile = adbfile.getSelectedFile();
//getPath()
try {
Runtime.getRuntime().exec(runfile.getParent());
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//This is where a real application would open the file.
System.out.println("File: " + runfile.getName() + ".");
} else {
JOptionPane.showMessageDialog(null, "Open command cancelled by user.");
}
System.out.println(returnVal);
}
};
}
最佳答案
在这一行:
runfile = adbfile.getSelectedFile(); // good
Runtime.getRuntime().exec(runfile.getParent());
// this will try to open your sh file's parent folder, it won't execute your sh file
应该是:
Runtime.getRuntime().exec(runfile.getAbsolutePath());
关于java - 尝试使用 JFileCoser 使用 adb 命令打开 ".SH"脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18772638/