File.getAbsolutePath在Ubuntu上不正确 [英] File.getAbsolutePath incorrect on Ubuntu

查看:354
本文介绍了File.getAbsolutePath在Ubuntu上不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题的后续问题(仅作简要说明:我已经能够运行双击OS X和Windows上的.jar文件,而不是Linux上的Java程序,因为在Linux上我遇到了文件路径问题.

This is a follow-up question to this one (just as a brief description: I have been able to run a Java program by double-clicking on the .jar file on OS X and Windows, but not on Linux, as with the latter I get a file path problem).

通过在Ubuntu(12.04)下使用NetBeans尝试一些操作,我发现问题似乎出在程序认为是其工作目录的位置(我从File.getAbsolutePath()的输出得出结论).如果我在NetBeans中启动我的应用程序,则一切正常(甚至在Ubuntu下),并且

Through trying out a few things using NetBeans under Ubuntu (12.04) I found that the problem seems to be located in what the program considers to be its working directory (which I concluded from the output of File.getAbsolutePath()). If I start my application in NetBeans, everything works (even under Ubuntu), and

System.out.println(new File(".").getAbsolutePath());

给我/home/my_home/projects/VocabTrainer/.,这是我的项目文件夹,因此是正确的.但是,如果我双击位于/home/my_home/projects/VocabTrainer/dist中的.jar文件,则在Ubuntu下突然得到的输出仅为/home/my_home/..这是有问题的,因为我想访问位于子目录中的数据文件.我的dist目录的目录.

gives me /home/my_home/projects/VocabTrainer/., which is my project folder and thus correct. However, if I double-click on the .jar file, located in /home/my_home/projects/VocabTrainer/dist, the output I get under Ubuntu suddenly is merely /home/my_home/. This is problematic as I want to access a data file which is located in a sub-directory of my dist dir.

有人知道这种行为的原因吗,我该如何解决这个问题?

Does anyone know the cause of this behaviour, and how I can solve the problem?

PS:我不知道这是否是必需的,但这是java -version

PS: I don't know if this is required, but here's the output of java -version

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)

推荐答案

目前的原因不是真的.但是由于明显的不可预测性,您可能不希望以这种方式处理它.假设您在下面的getResource调用中使用jar中某些内容的合格类名,则应获得类似文件的内容.

The cause, not really, at the moment. But you may not want to handle it that way, due to the evident unpredictability. Something like this should get the file, assuming you use the qualified class name of something in the jar in the below getResource call.:

URL url = this.getClass().getClassLoader().getResource("thepackage/ofyourclass/JunkTest.class");  //get url of class file.  expected: ("jar:file:/somepath/dist/yourjar.jar!qualified/class/name.class")
File distDir = null;
if(url.getProtocol() == "jar") {
    String classPath = null;
    String jarPath = url.getPath();
    if(jarPath.matches(".*:.*")) jarPath = new URL(jarPath).getPath();
    classPath = jarPath.split("!")[0];
    distDir = new File(classPath).getParentFile(); //may need to replace / with \ on windows?
} else { //"file" or none
    distDir = new File(url.toURI()).getParentFile();
}    
//... do what you need to do with distDir to tack on your subdirectory and file name

我应该指出,这显然是不可靠的.您可能能够在启动时直接将文件的位置添加到类路径中(或将要查找的文件包含在jar中).由此,您可以直接将this.getClass().getClassLoader().getResource()与您要查找的文件名一起使用,这将使您得到类似的东西:

I should point out this is clearly hacky. You may be able to add the file's location to the classpath directly at startup (or include the file you're looking for in the jar). From this you could use this.getClass().getClassLoader().getResource() with the filename of what you're looking for directly, which would get you to something like:

URL url = this.getClass().getResource("yourfile");
File file = new File(url.toURI());
//... use file directly from here

进一步好的,适合您所缺少的协议,并将其散布开来,因此错误消息对您而言更易读.

FURTHER ok, adapted to your missing protocol, and spread it out, so error messages will be more readable for you.

这篇关于File.getAbsolutePath在Ubuntu上不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆