回答
假设您希望启动Windows程序notepad.exe
,您正在寻找exec
函数。你可能想调用是这样的:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\path\\to\\notepad.exe C:\\path\\to\\file.txt");
例如,在我的机器记事本是位于C:\Windows\notepad.exe
:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe C:\\test.txt");
这将打开记事本的文件test.txt打开编辑。
注意,您还可以指定exec
这是要执行的工作目录的第三个参数 - 因此,您可以启动相对于程序工作目录存储的文本文件。
使用SWT,你可以启动任何 如果你想模拟双击windows中的文本,这是不可能的,只有一个普通的JRE。您可以使用本机库一样SWT和使用下面的代码打开一个文件:
org.eclipse.swt.program.Program.launch("c:\path\to\file.txt")
如果你不希望使用第三方lib中,你应该知道,你知道在哪里的notepad.exe是(或者它在路径中可见):
runtime.exec("notepad.exe c:\path\to\file.txt");
阿帕奇common-exec是处理外部进程执行一个好的图书馆。
更新:更完整的回答你的问题,可以发现here
尝试
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().edit(file);
} else {
// dunno, up to you to handle this
}
确保该文件存在。感谢Andreas_D指出了这一点。
您可能想要添加有关所需Java版本的详细信息以便使其工作。 – 2010-08-15 12:39:16
嘿,不知道。凉。但是 - 第二行必须阅读'Desktop.getDesktop()。edit(file);'。并且该文件必须被创建,否则它会抱怨'IllegalArgumentException'。 – 2010-08-15 12:42:51
谢谢@Andreas,修复它。确保文件存在是guilgamos的工作的一部分:)无论如何,+1还是不错的 – whiskeysierra 2010-08-15 15:35:16
在IDE(Eclipse)中,它包含关于“C:\ path \ to \ notepad.exe C:\ path \ to \ file.txt”。 所以我用了以下这个工作让我保持我和我的IDE快乐:o) 希望这会帮助其他人。
String fpath;
fPath =System.getProperty("java.io.tmpdir")+"filename1" +getDateTime()+".txt";
//SA - Below launches the generated file, via explorer then delete the file "fPath"
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("explorer " + fPath);
Thread.sleep(500); //lets give the OS some time to open the file before deleting
boolean success = (new File(fPath)).delete();
if (!success) {
System.out.println("failed to delete file :"+fPath);
// Deletion failed
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
(假设你想记事本打开“myfile.txt的” :)
ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
String fileName = "C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\Student Project\\src\\studentproject\\resources\\RealWorld.chm";
String[] commands = {"cmd", "/c", fileName};
try {
Runtime.getRuntime().exec(commands);
//Runtime.getRuntime().exec("C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\SwingTest\\src\\Test\\RealWorld.chm");
} catch (Exception ex) {
ex.printStackTrace();
}
如果你开始用命令的命令行记事本你可以做到这一点,最好的:记事本开始
String[] startNotePadWithoutAdminPermissions = new String[] {"CMD.EXE", "/C", "start" "notepad" };
保存数组的字符串命令,并给它像参数在exec
Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2);
runtimeProcess.waitFor();
- 1. 如何在java中的记事本中打开.log文件?
- 2. 如何启动记事本++在空中打开文本文件?
- 3. 如何用C++在记事本++中打开文件?
- 4. 如何在Windows PowerShell中打开记事本++ python文件?
- 5. 如何在.NET中的记事本中打开文本?
- 6. 在记事本中以文本文件格式打开资源
- 7. 如何在批处理文件中用记事本打开文件
- 8. 打开记事本
- 9. 如何用记事本++打开一个类文件?
- 10. 如何从批处理文件中打开两个记事本文件?
- 11. 从程序在记事本++中打开Python文件
- 12. C#双击文件在记事本标签中打开
- 13. 使用VB.net在记事本++中打开多个TXT文件
- 14. 在记事本中打开VS2010 cs文件
- 15. Powershell dot sourcing在记事本中打开文件
- 16. 使用VBA在记事本中打开.tbl文件
- 17. 在c中用记事本++阅读的打开文件#
- 18. C# - 用记事本打开文本文件最小化?
- 19. 如何在Java中打开文本文件
- 20. 如何根据文件名中的最大数字从批处理文件在记事本中打开文件?
- 21. 强制超链接打开记事本中的目标文件
- 22. 当文件在记事本中打开时,python可以写入文件吗?
- 23. 在记事本中打开ini文件和配置文件的VBScript
- 24. 如何通过按钮在c#中打开记事本
- 25. 如何在记事本++中打开整个Rails应用程序
- 26. 记事本+ +打开文件的废弃版本
- 27. 如何在java中制作记事本?
- 28. 我想用C++中的记事本打开一个文件,但是如何?
- 29. 如何在批处理文件中打开批处理文件,但在记事本中?
- 30. 如何在内存中制作文本文件并在其上写入内容并在客户端打开记事本并打开该文件?
“记事本”是什么意思?在Windows上使用一个糟糕的文本编辑程序,或TextArea控件?原谅我承担“事情”,但听起来你不知道Swing/AWT的基础知识。 – aviraldg 2010-08-15 11:24:51
...你想打开记事本程序,或者你在记事本中创建的文本文件吗? – Stephen 2010-08-15 11:25:07