Jar文件名表单java代码

Jar文件名表单java代码,java,jar,Java,Jar,我想从java代码中确定jar文件名。我在谷歌上找到了很多解决方案,但都不管用。我在这里尝试了一个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位服务器虚拟机(构建17.1-b03-307,混合模式) 谢谢你的帮助 编辑: 我编辑我

我想从java代码中确定jar文件名。我在谷歌上找到了很多解决方案,但都不管用。我在这里尝试了一个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位服务器虚拟机(构建17.1-b03-307,混合模式)

谢谢你的帮助


编辑: 我编辑我的帖子来回答评论。
当我想在路径中System.out.println时,某些解决方案会给我“null”值,当我想创建文件实例时,也会失败。
其他解决方案当我询问路径时,他们没有给出类似于
file://…
,而是给出类似于
rsch://
或类似的内容,我不太清楚,但这是一个4个字符的简单单词


编辑2: 我从控制台运行一个可执行jar。我希望在执行的jar文件中的类中有这个jar文件名


编辑3:
4个字符的单词是:
rsrc:./

编码我是如何得到这个的:

    File file = null;
    try {
        System.out.println(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

编辑4: 我还尝试了以下代码:

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中完美地运行,但是当我创建可运行的jar文件时,我认为您可以看到问题所在。

这就是您想要的吗


为了访问无论jar文件是否存在都能正常工作的资源,我总是使用classname.class.getResourceAsStream()。但是链接的文档显示了如何将JarFile()用于相同的目的。

好的,最后是解决我问题的代码:

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();
}

通过这种方式,它可以找到我的属性文件。

您发布的链接提供了一些解决方案。你说“什么都不管用”-你能详细解释一下它们为什么不管用吗?Jar文件名是什么?编辑之后,我认为这个问题不值得-1。谢谢你改变主意!我不是第一个给出-1的人。。。只是让那个人重新考虑一下。你的问题写得不是特别糟糕或不清楚,是我提的。您仍然使用File();如果改用JarFile(),则可以使用JarEntry.toString()访问属性。在任何情况下,都可以使用getResourceAsStream(),它简单可靠。
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();
}