繁体   English   中英

Jar文件名表单java代码

[英]Jar file name form java code

我想从我的java代码中确定jar文件名。 我在谷歌找到了很多解决方案,但没有任何效果。 只是为了看看我在这里尝试的是一个stackoverflow论坛,其中发布了一堆解决方案: stackoverflow

我有Mac OS X 10.6.5。
当我输入java -version时,我得到了这个结果:
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”值,当我想创建File的实例时也失败。
其他解决方案,当我要求他们没有提供类似file:/.....的路径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中运行,但是当我创建runnable jar文件时,我认为你可以看到这个问题。

这是你想要的吗? http://www.uofr.net/~greg/java/get-resource-listing.html


jcomeau@intrepid:/tmp$ cat test.java; javac test.java; jar cvf test.jar test.class; java -cp test.jar test
public class test {
 public static void main(String[] args) {
  System.out.println(test.class.getResource("test.class"));
 }
}
adding: META-INF/ (in=0) (out=0) (stored 0%)
adding: META-INF/MANIFEST.MF (in=56) (out=56) (stored 0%)
adding: test.class (in=845) (out=515) (deflated 39%)
Total:
------
(in = 885) (out = 883) (deflated 0%)
jar:file:/tmp/test.jar!/test.class

为了访问无论是否存在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();
}

通过这种方式,它创建了我的属性文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM