Launch file from Java

北城余情 提交于 2019-12-04 01:01:26

问题


I want to launch a file(a document) from a Java program and phase the following requirements:

  • Method must be applicabale on Mac, Win and Linux systems
  • I am not allowed to use "Runtime.getRuntime().exec("cmd.exe /C +"filename");
  • The file I am launching needs to be either of .doc / .docx / .rtf

The file is created runtime, a result from a report being created. Any good practices?


回答1:


Use Java Desktop API.

Desktop.getDesktop().open(new File(yourfilename));



回答2:


If you're running 1.6, use the Desktop API per mad-j's advice. If you're using an older VM (1.5 or earlier) you'll need to write your own platform-specific code to do this.

On the mac,

Runtime.getRuntime().exec(new String[] {"open", pathToFile});

On windows,

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/C", pathToFile});

You may need to escape the path on windows.



来源:https://stackoverflow.com/questions/847838/launch-file-from-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!