[英]Cross-platform way to open a file using Java 1.5
我正在使用Java 1.5,我想啟動相關的應用程序來打開文件。 我知道Java 1.6引入了Desktop API ,但我需要一個Java 1.5的解決方案。
到目前為止,我找到了一種在Windows中執行此操作的方法:
Runtime.getRuntime().exec(new String[]{ "rundll32",
"url.dll,FileProtocolHandler", fileName });
有沒有跨平台的方式來做到這一點? 或者至少是Linux的類似解決方案?
public static boolean isWindows() {
String os = System.getProperty("os.name").toLowerCase();
return os.indexOf("windows") != -1 || os.indexOf("nt") != -1;
}
public static boolean isMac() {
String os = System.getProperty("os.name").toLowerCase();
return os.indexOf("mac") != -1;
}
public static boolean isLinux() {
String os = System.getProperty("os.name").toLowerCase();
return os.indexOf("linux") != -1;
}
public static boolean isWindows9X() {
String os = System.getProperty("os.name").toLowerCase();
return os.equals("windows 95") || os.equals("windows 98");
}
和
if (isLinux())
{
cmds.add(String.format("gnome-open %s", fileName));
String subCmd = (exec) ? "exec" : "openURL";
cmds.add(String.format("kfmclient "+subCmd+" %s", fileName));
}
else if (isMac())
{
cmds.add(String.format("open %s", fileName));
}
else if (isWindows() && isWindows9X())
{
cmds.add(String.format("command.com /C start %s", fileName));
}
else if (isWindows())
{
cmds.add(String.format("cmd /c start %s", fileName));
}
JDIC是一個在Java 1.5中提供類似桌面功能的庫。
這個答案 +1
另外,我建議使用多態的以下實現:
這樣,您可以通過減少類之間的耦合來更輕松地添加新平台。
客戶代碼:
Desktop desktop = Desktop.getDesktop();
desktop.open( aFile );
desktop.imaginaryAction( aFile );
桌面impl:
package your.pack.name;
import java.io.File;
public class Desktop{
// hide the constructor.
Desktop(){}
// Created the appropriate instance
public static Desktop getDesktop(){
String os = System.getProperty("os.name").toLowerCase();
Desktop desktop = new Desktop();
// This uf/elseif/else code is used only once: here
if ( os.indexOf("windows") != -1 || os.indexOf("nt") != -1){
desktop = new WindowsDesktop();
} else if ( os.equals("windows 95") || os.equals("windows 98") ){
desktop = new Windows9xDesktop();
} else if ( os.indexOf("mac") != -1 ) {
desktop = new OSXDesktop();
} else if ( os.indexOf("linux") != -1 && isGnome() ) {
desktop = new GnomeDesktop();
} else if ( os.indexOf("linux") != -1 && isKde() ) {
desktop = new KdeDesktop();
} else {
throw new UnsupportedOperationException(String.format("The platform %s is not supported ",os) );
}
return desktop;
}
// default implementation :(
public void open( File file ){
throw new UnsupportedOperationException();
}
// default implementation :(
public void imaginaryAction( File file ){
throw new UnsupportedOperationException();
}
}
// One subclass per platform below:
// Each one knows how to handle its own platform
class GnomeDesktop extends Desktop{
public void open( File file ){
// Runtime.getRuntime().exec: execute gnome-open <file>
}
public void imaginaryAction( File file ){
// Runtime.getRuntime().exec:gnome-something-else <file>
}
}
class KdeDesktop extends Desktop{
public void open( File file ){
// Runtime.getRuntime().exec: kfmclient exec <file>
}
public void imaginaryAction( File file ){
// Runtime.getRuntime().exec: kfm-imaginary.sh <file>
}
}
class OSXDesktop extends Desktop{
public void open( File file ){
// Runtime.getRuntime().exec: open <file>
}
public void imaginaryAction( File file ){
// Runtime.getRuntime().exec: wow!! <file>
}
}
class WindowsDesktop extends Desktop{
public void open( File file ){
// Runtime.getRuntime().exec: cmd /c start <file>
}
public void imaginaryAction( File file ){
// Runtime.getRuntime().exec: ipconfig /relese /c/d/e
}
}
class Windows9xDesktop extends Desktop{
public void open( File file ){
//Runtime.getRuntime().exec: command.com /C start <file>
}
public void imaginaryAction( File file){
//Runtime.getRuntime().exec: command.com /C otherCommandHere <file>
}
}
這只是一個例子,在現實生活中不值得創建一個新類只是為了參數化一個值(命令字符串%s)但是讓我們想象每個方法以特定於平台的方式執行另一個步驟。
采取這種方法,可能會刪除不需要的if / elseif / else結構,隨着時間的推移可能會引入錯誤(如果代碼中有6個並且更改是neede,您可能忘記更新其中一個,或者通過copy /粘貼你可能會忘記更改命令來執行)
同樣作為補充:使用xdg-open
而不是gnome-open
xdg-open
。 它是XdgUtils的一部分,它們又是LSB Desktop支持包的一部分(從3.2開始)。
你可以(應該)仍然使用gnome-open
作為后備,但xdg-open
也適用於非GNOME桌面。
通過SWT ,您可以通過以下方式獲取標准程序以打開文件:
final Program p = Program.findProgram(fileExtension);
p.execute(file.getAbsolutePath());
嚴格來說,這不是跨平台,因為SWT是依賴於平台的,但是對於每個平台,您都可以使用不同的SWT jar。
您可以使用操作系統默認方式為您打開它。
另一個答案(由boutta提出)建議使用SWT。 我不建議僅為此目的引用庫,但如果您已經使用它,只需執行:
Program.launch("http://google.com/");
請注意,如果已創建Display
對象(例如,通過創建Shell
),此方法將僅起作用(並返回true
)。 還要注意它必須在主線程中運行; 例如:
Display.syncExec(new Runnable() {
public void run() {
Program.launch("http://google.com/");
}
});
在上面的示例中,我已經啟動了一個URL,但啟動文件的工作方式相同。
我們確實將命令放在配置文件中的某個位置。
您的“JAR和源代碼”將是“跨平台”,但您的部署不會。
你也可以做這樣的回答 。 您可以將“Deskop”實現的工廠類的類名放入安裝文件中。 (如果你願意,可以是導游或春天)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.