Mac OS X上的Java System.getProperty(" user.dir") [英] Java System.getProperty("user.dir") on Mac OS X

查看:164
本文介绍了Mac OS X上的Java System.getProperty(" user.dir")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在桌面上的Mac OS X 10.4上有一个应用程序包。我的应用程序查找名为resources的文件夹,其中保存要显示的文件(与可运行的JAR保持在同一位置)。我知道应用程序包中还有一个名为Resources的文件夹,抱歉,如果那令人困惑,但我从来没有在Mac上编程,也不知道这将是同一个名字。

I have an application bundle on Mac OS X 10.4 on the desktop. My application looks for a folder named "resources" in which files to be displayed are kept (kept in the same location as the runnable JAR). I know there is a folder named "Resources" within the app bundle too, sorry if thats confusing, but I never programmed on a Mac and didnt know this would be the same name.

在Windows中,当我调用 System.getProperty(user.dir)时,我得到的位置找到可运行的JAR文件。正是我想要的。

In Windows, when I call System.getProperty("user.dir") I get the location at which the runnable JAR file is located. Exactly what I wanted.

为什么当我运行应用程序包时,getProperty返回/?就这样。我希望它返回类似/Users/user_name/Desktop......这是我的应用程序包所在的位置。

Why then when I run an application bundle is the getProperty returning "/"? Thats all. I expected it to return something like "/Users/user_name/Desktop"...which is where my app bundle is located.

推荐答案

这是因为user.dir表示运行JVM时生效的当前用户目录;在Windows中,除非另行指定,否则这通常是JAR的位置。在OSX中可能没有当前目录的概念,但更可能的是它只有不同的默认值。

That's because "user.dir" indicates the current user directory in effect when the JVM is run; in Windows, this is often the location of the JAR unless you specify otherwise. In OSX there may well be no concept of a current dir, but more likely it just has a different default.

虽然我从未在OSX下专门测试过此代码,但是可以尝试这个来找到加载任何类的目录:

Though I have never specifically tested this code under OSX, you can try this to locate the directory from which any class was loaded:

static public File getClassLocation(Class cls, boolean trmjar) {
    ClassLoader                         clsldr;                                                     // class loader
    URL                                 urlobj;                                                     // url object
    String                              exturl;                                                     // external form of URL
    String                              lwrurl;                                                     // lowercase external form of URL
    File                                rtnfil;                                                     // return file

    if((clsldr=cls.getClassLoader())==null) { clsldr=ClassLoader.getSystemClassLoader(); }

    if((urlobj=clsldr.getResource(cls.getName().replace('.','/')+".class"))==null) {
        return null;
        }

    exturl=urlobj.toExternalForm();
    lwrurl=exturl.toLowerCase();
    while(lwrurl.startsWith("jar:") || lwrurl.startsWith("file:/")) {
        if(lwrurl.startsWith("jar:")) {
            if(lwrurl.indexOf("!/")!=-1) { exturl=exturl.substring(4,(exturl.indexOf("!/"))); }     // strip encapsulating "jar:" and "!/..." from JAR url
            else                         { exturl=exturl.substring(4                       ); }     // strip encapsulating "jar:"
            }
        if(lwrurl.startsWith("file:/")) {
            exturl=exturl.substring(6);                                                             // strip encapsulating "file:/"
            if(!exturl.startsWith("/")) { exturl=("/"+exturl); }
            while(exturl.length()>1 && exturl.charAt(1)=='/') { exturl=exturl.substring(1); }
            }
        lwrurl=exturl.toLowerCase();
        }
    exturl=java.net.URLDecoder.decode(exturl,"UTF-8");
    rtnfil=new File(exturl);
    if(lwrurl.endsWith(".class") || (trmjar && lwrurl.endsWith(".jar"))) { rtnfil=rtnfil.getParentFile(); }
    if(rtnfil.exists()) { rtnfil=rtnfil.getAbsoluteFile(); }
    return rtnfil;
    }

自从Java以来​​,它在Windows下为所有版本的Java可靠地工作了多年1。

it's worked reliably for me for years under Windows for all versions of Java since Java 1.

这篇关于Mac OS X上的Java System.getProperty(" user.dir")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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