您好我试图从我的jar文件旁边的文件夹中运行/打开一个快捷方式。我似乎无法让它继续工作,因为它一直在说
系统找不到指定的文件
你能指出我做错了吗?
JButton btnInstall = new JButton("Install/Run");
btnInstall.setEnabled(false);
btnInstall.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ChangePath(ProgramAdder.programs.get(count).ProgramPath.toString());
System.out.print(ProgramAdder.programs.get(count).ProgramPath.toString());
}
});
btnInstall.setBounds(173, 226, 116, 45);
frame.getContentPane().add(btnInstall);
public void ChangePath(String Path){
try {
Process process= Runtime.getRuntime().exec(Path);
} catch (IOException e) {
e.printStackTrace();
}
}
package Logic;
import java.util.ArrayList;
import java.util.List;
public class ProgramAdder {
public static List<Programs> programs = new ArrayList<Programs>( );
public static void ProgramList(){
ProgramAdder.addProgram("HD Sentinel","/Shortcuts In Order/1 - HDSentinel.lnk");
ProgramAdder.addProgram("Defraggler","/Shortcuts In Order/2 - Defraggler.lnk");
ProgramAdder.addProgram("CC Cleaner","/Shortcuts In Order/3 - CCCLEANER.lnk");
ProgramAdder.addProgram("Hit Man Pro 64Bit","/Shortcuts In Order/4 - HitManPro 32bit.lnk");
ProgramAdder.addProgram("Hit Man Pro 32Bit","/Shortcuts In Order/4 - HitManPro 64bit.lnk");
ProgramAdder.addProgram("Super Anti Spyware","/Shortcuts In Order/5 - SuperAntiSpyware.lnk");
ProgramAdder.addProgram("Malware Bytes","/Shortcuts In Order/6 - MalwareBytes.lnk");
}
public static void addProgram(String ProgramName,String ProgramPath){
Programs p = new Programs();
p.ProgramName = ProgramName;
p.ProgramPath = ProgramPath;
programs.add(p);
}
}
我尝试在main中运行此功能,但我仍然得到相同的错误
package Logic;
import java.io.IOException;
public class OpenTets {
public static void OpenProgram(){
try {
Process process= Runtime.getRuntime().exec("/Shortcuts In Order/1 - HDSentinel.lnk");
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
如果您不需要保留Process
返回的Runtime.exec
对象,那么您可以使用更简单的Desktop.getDesktop().open
。提供您想要打开的File
。