Java:打开文件(Windows+;Mac)
可能重复:Java:打开文件(Windows+;Mac),java,file,pdf,multiplatform,Java,File,Pdf,Multiplatform,可能重复: 我有一个打开文件的java应用程序。这在windows上非常有效,但在mac上却不行 这里的问题是我使用windows配置打开它。代码是: Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler”+文件) 现在我的问题是,在mac中打开它的代码是什么?还是有其他方法可以打开一个多平台的PDF 编辑: 我创建的文件如下所示: File folder = new File("./files"); File[] li
我有一个打开文件的java应用程序。这在windows上非常有效,但在mac上却不行 这里的问题是我使用windows配置打开它。代码是:
Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler”+文件)代码>
现在我的问题是,在mac中打开它的代码是什么?还是有其他方法可以打开一个多平台的PDF
编辑:
我创建的文件如下所示:
File folder = new File("./files");
File[] listOfFiles = folder.listFiles();
在循环中,我将它们添加到数组中:
fileArray.add(listOfFiles[i])代码>
如果我尝试使用Desktop.getDesktop().open(file)从该数组打开一个文件,它会说找不到该文件(路径混乱,因为我将“/files”用作文件夹)您需要查看命令以便
由Martijn编辑
如果在文件路径中使用空格,则效果更好:
Runtime.getRuntime().exec(new String[]{"/usr/bin/open", file.getAbsolutePath()});
首先,与*.dll相关的任何内容都是windows版本
也许您可以在Linux上尝试下面的代码,它也可能在MAC上工作:
import java.awt.Desktop;
import java.io.File;
Desktop d = Desktop.getDesktop();
d.open(new File("foo.pdf"))
这是一个操作系统检测器:
public class OSDetector
{
private static boolean isWindows = false;
private static boolean isLinux = false;
private static boolean isMac = false;
static
{
String os = System.getProperty("os.name").toLowerCase();
isWindows = os.contains("win");
isLinux = os.contains("nux") || os.contains("nix");
isMac = os.contains("mac");
}
public static boolean isWindows() { return isWindows; }
public static boolean isLinux() { return isLinux; }
public static boolean isMac() { return isMac; };
}
然后您可以像这样打开文件:
public static boolean open(File file)
{
try
{
if (OSDetector.isWindows())
{
Runtime.getRuntime().exec(new String[]
{"rundll32", "url.dll,FileProtocolHandler",
file.getAbsolutePath()});
return true;
} else if (OSDetector.isLinux() || OSDetector.isMac())
{
Runtime.getRuntime().exec(new String[]{"/usr/bin/open",
file.getAbsolutePath()});
return true;
} else
{
// Unknown OS, try with desktop
if (Desktop.isDesktopSupported())
{
Desktop.getDesktop().open(file);
return true;
}
else
{
return false;
}
}
} catch (Exception e)
{
e.printStackTrace(System.err);
return false;
}
}
回答您的编辑问题:
尝试使用file.getAbsoluteFile()
甚至file.getCanonicalFile()
我在Mac OS X.d上工作,当你声明它时它是什么类型的对象?顺便说一句,我试过这个,它给出了一个错误,它找不到我的变量。注意我问题中的编辑,你可以看到我是如何创建文件的。@Mi Mee:因为他不知道d
是什么,所以我编辑了一下你的答案以帮助操作。为了安全起见,还可以调用Desktop.isDesktopSupported()
。请记住,它只是java 6。当我运行它时,它会给出一个IOException`无法运行程序“open”:找不到给定的文件“。然而,我问题中的代码工作起来很有魅力。请尝试使用OpenPath的完整路径。我如何知道OpenPath的完整路径?它必须在多平台上工作,开放平台在每个操作系统上的位置不是都不同吗?你有my+1,它在Mac OS X上工作。可以检查系统是Mac还是windows吗?我正在windows atm上测试它,因为它应该可以在两种平台上运行。为什么在Linux/OS X上使用exec
的String[]
变体,而不是windows?Martin,刚刚在我的一台旧Mac上测试了这个,在那里它不起作用。它表示不支持ClassVersion
。我应该用另一个东西吗?我在OS Detecter中更改了Mac的条件,请再试一次。讽刺的是,toLowercase()
应该是toLowercase()
,但它不允许我编辑你的帖子。此外,对于windows,您应该将rundll和其余部分分开,因此:“rundll32”、“url.dll、FileProtocolHandler”
,您编写它的方式会产生错误,因为没有“rundll32 url.dll、FileProtocolHandler.exe”
public static boolean open(File file)
{
try
{
if (OSDetector.isWindows())
{
Runtime.getRuntime().exec(new String[]
{"rundll32", "url.dll,FileProtocolHandler",
file.getAbsolutePath()});
return true;
} else if (OSDetector.isLinux() || OSDetector.isMac())
{
Runtime.getRuntime().exec(new String[]{"/usr/bin/open",
file.getAbsolutePath()});
return true;
} else
{
// Unknown OS, try with desktop
if (Desktop.isDesktopSupported())
{
Desktop.getDesktop().open(file);
return true;
}
else
{
return false;
}
}
} catch (Exception e)
{
e.printStackTrace(System.err);
return false;
}
}