简体   繁体   中英

Jar file name form java code

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

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)

Thank you for your help.


Edit: 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.


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.


Edit 3:
The 4 character word is: rsrc:./

Code how I got this:

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

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

    }

With this result:

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

This code perfectly runs in the eclipse, but when I create the runnable jar file I think you can see the problem.

Is this what you want? 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

For access to resources that works regardless of the presence of a jar file, I always use classname.class.getResourceAsStream(). But the linked document shows how to use JarFile() for the same purpose.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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