2011-01-10 58 views
1

对于在清单带有主类可执行的JAR:可执行的JAR安装路径

当我使用Java的罐子myjar.jar,我怎么能找到这个罐子在运行时的安装目录它启动?

我想要做的是开发一个命令行客户端Flyway

INSTALLATION_PATH 
| 
-- bin 
| | 
| --start.sh (launches flyway.jar) 
| 
-- lib 
| | 
| --flyway.jar (contains Main class, loads flyway.properties) 
| 
-- conf 
    | 
    --flyway.properties (configuration) 

如何flyway.jar解决安装路径:

该工具将具有下列文件夹结构安装?

回答

6

你可以试试:

// this generally returns the PWD 
String pwd = System.getProperties().getProperty("user.dir"); 

// or you can get the location of the URL 
// from the .jar file in which your class was loaded 
// you may want to then simply back it up a level with ".." 
URL yourJar = getClass().getProtectionDomain().getCodeSource().getLocation(); 
1

下面会给你的起始目录:

new File(".").getAbsolutePath() 
+1

诚然,这不过假设安装路径=当前目录下,这可能并不总是正确。 – 2011-01-10 22:30:37

0

这样做,而不是试图找出在运行安装目录下的优选方式是这样的:
做安装,然后编辑系统类路径属性添加此jar和任何依赖jar。
之后,您可以从任何目录运行它。
当然,这样做的好处是,您只需在安装时设置类路径一次。每次决定运行时都不需要在运行时查找安装目录。

+0

我的问题不是我无法找到jar,而是jar必须找到属性。 – 2011-01-10 22:29:05