简体   繁体   English

运行应用程序的Java代码

[英]Java code to run applications

I need a java code to allow me to run this notepad and open a specified file, for example : 我需要一个java代码来允许我运行这个记事本并打开一个指定的文件,例如:

String []a={"C:/Users/day/Desktop/a.txt"};
Process p = Runtime.getRuntime().exec("notepad",a);

This code runs notepad but does not open the file a.txt . 此代码运行记事本但不打开文件a.txt
What can be the problem ? 可能是什么问题?

The second argument in exec represents the environmental variables. exec的第二个参数表示环境变量。 You want 你要

String[] a = { "notepad", "C:/Users/day/Desktop/a.txt" };
Process p = Runtime.getRuntime().exec(a);

You need double slashes in your file name, and all of your command args can be in the same array. 您的文件名中需要双斜杠,并且所有命令参数都可以在同一个数组中。

String[] args = {"notepad.exe", "C://Users//day//Desktop//a.txt"};
Process p = Runtime.getRuntime().exec(args);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM