如何知道.jar文件目录 [英] how to know .jar file directory

查看:234
本文介绍了如何知道.jar文件目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

文件系统上的哪个地方是从我的java类加载的?

如何获取当前正在执行的jar文件所在的目录?

我如何知道程序中的.jar文件目录
i希望使用这个目录在用户保存这个.jar文件的同一目录中创建另一个文件

How can i know .jar file directory within my program i want to use this directory to create another files in same directory which user save this .jar file

推荐答案

我不要认为你可以很容易地找到JAR文件的位置,但你可以找到你的程序运行目录:

I don't think you can easily find out where the JAR file is, but you can find out which directory your program is being run from:

File cwdFile = new File (".");
String cwd = cwdFile.getAbsolutePath();

这仅适用于用户实际从其所在目录运行JAR文件的情况:

This only works if the user actually runs the JAR file from the directory it's in:

java -jar MyExectuableJar.jar

如果他们从另一个目录运行它,如下所示:

If they run it from another directory, like this:

java -jar /usr/bin/java/MyExecutableJar.jar

然后您将获得当时用户碰巧所在的目录。

then you'll get whichever directory the user happens to be in at the time.

这篇关于如何知道.jar文件目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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