2024-04-24 21:49:49 | 我爱编程网
使用ProcessBuilder类操作。
import java.io.BufferedReader;
import java.io.InputStream;我爱编程网
import java.io.InputStreamReader;
import java.util.LinkedList;
public class TestProcessBuilder
{
public static void main ( String[] args ) throws Exception
{
ProcessBuilder builder = new ProcessBuilder ("cmd", "/c", "notepad.exe a.txt");
Process process = builder.start ();
InputStream is = process.getInputStream ();
InputStreamReader isr = new InputStreamReader (is, "GBK");
BufferedReader br = new BufferedReader (isr);
String line;
while (null != ( line = br.readLine () ))
{
System.out.println (line);
}
br.close ();
isr.close ();
is.close ();
process.destroy ();
}
}
public class One
{
public static void main(String[] args) throws Exception
{
String path=One.class.getClassLoader().getResource("com/one/Decima.exe").getFile();
path = java.net.URLDecoder.decode(path,"utf-8");
Runtime.getRuntime().exec(path.substring(1));
String path3=One.class.getClassLoader().getResource("com/one/SQL.chm").getFile();
path3 = java.net.URLDecoder.decode(path3,"utf-8");
Runtime.getRuntime().exec("hh.exe " +path3.substring(1));
}
}
两个例子 需要这句话path3 = java.net.URLDecoder.decode(path3,"utf-8"); 是防止路径有空格
如果保证每空格 可以去掉
public class transferExe {\x0d\x0a public static void main(String[] args) {\x0d\x0a openWinExe();\x0d\x0a openExe();\x0d\x0a }\x0d\x0a //用 Java 调用windows系统的exe文件,比如notepad,calc之类\x0d\x0a public static void openWinExe() {\x0d\x0a Runtime rn = Runtime.getRuntime();\x0d\x0a Process p = null;\x0d\x0a try {\x0d\x0a String command = "notepad";\x0d\x0a p = rn.exec(command);\x0d\x0a } catch (Exception e) {\x0d\x0a System.out.println("Error win exec!");\x0d\x0a }\x0d\x0a }\x0d\x0a //调用其他的可执行文件,例如:自己制作的exe,或是 下载 安装的软件.\x0d\x0a public static void openExe() {\x0d\x0a Runtime rn = Runtime.getRuntime();\x0d\x0a Process p = null;\x0d\x0a try {\x0d\x0a p = rn.exec("\"D:/QQ2010.exe\"");\x0d\x0a } catch (Exception e) {\x0d\x0a System.out.println("Error exec!");\x0d\x0a }\x0d\x0a }\x0d\x0a }
2024-01-01 09:58:48
2024-04-13 00:38:12
2024-03-31 15:14:06
2024-03-21 00:22:13
2024-02-10 22:15:59
2024-04-11 19:37:19