Jar文件名表单java代码 [英] Jar file name form java code
问题描述
我想从我的java代码中确定jar文件名。我在谷歌找到了很多解决方案,但没有任何效果。只是为了看看我在这里尝试的是一个stackoverflow论坛,其中发布了一堆解决方案: stackoverflow
I would like to determine the jar file name from my java code. I found many solutions in the google, but nothing works. Just to see what I tried here is a stackoverflow forum where a bunch of solutions is posted: stackoverflow
我有Mac OS X 10.6.5。
当我输入java -version时得到这个结果:
java版本1.6.0_22
Java(TM)SE运行时环境(版本1.6.0_22-b04-307-10M3261)
Java HotSpot(TM)64位服务器VM(版本17.1-b03-307,混合模式)
I have Mac OS X 10.6.5.
When I type java -version I get this result:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03-307, mixed mode)
感谢您的帮助。
编辑:
编辑我的帖子以回答评论。
当我想要System.out.println路径时,某些解决方案给我null值,当我想创建文件实例时,也会失败。
其他解决方案时我问他们没有提供类似文件的路径:/ .....
,而是提供类似 rsch的内容:/
或类似的东西,我不确切知道,但它是一个4个字符的简单单词。
I edit my post to answer for the comment.
Some of the solutions gives me "null" value when I want to System.out.println the path and also fails when I want to create an instance of a File.
Other solutions when I ask for the path they don't give something like file:/.....
, instead they give something like rsch:/
or something like, this I don't know exactly, but it is a 4 character simple word.
编辑2:
我从控制台运行一个可执行jar。我想在执行的jar文件中的类中有这个jar文件名。
Edit 2: I run an executable jar from the console. And I would like to have this jar file name in the classes which are in the executed jar file.
编辑3:
4个字符的单词是: rsrc:./
代码如何得到这个:
File file = null;
try {
System.out.println(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
编辑4:
我也试过这段代码:
Edit 4: I also tried this code:
package core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class MyClass {
public String getText(String key) {
String path = "" + MyClass.class.getResource("../any.properties");
File file = new File((path).substring(5, path.length()));
Properties props = readProps(file);
return props.getProperty(key);
}
private Properties readProps(File file) {
Properties props = new Properties();
InputStream in = null;
try {
in = new FileInputStream(file);
props.load(in);
in.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
public static void main(String[] args) {
System.out.println(new MyClass().getText("anything"));
}
}
结果如下:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1937)
at core.PropHandler.getText(MyClass.java:14)
at core.PropHandler.main(MyClass.java:39)
... 5 more
此代码完美运行在eclipse中,但是当我创建runnable jar文件时,我认为你可以看到问题。
This code perfectly runs in the eclipse, but when I create the runnable jar file I think you can see the problem.
好的,最后这是解决我问题的代码:
Ok, finally this is the code which resolved my problem:
String sConfigFile = "any.properties";
InputStream in = MyClass.class.getClassLoader().getResourceAsStream(sConfigFile);
if (in == null) {
System.out.println("ugly error handling :D");
}
Properties props = new java.util.Properties();
try {
props.load(in);
} catch (IOException e) {
e.printStackTrace();
}
通过这种方式,它找到了我的属性文件。
With this way it founds my property file.
这篇关于Jar文件名表单java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!