3

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.

7
  • 1
    The link you posted offers a few solutions. You say "nothing works" - can you elaborate on why they don't work? Commented Nov 27, 2010 at 23:38
  • Jar file name of what? Commented Nov 27, 2010 at 23:42
  • After the edits, I don't think the question deserves a -1. Commented Nov 27, 2010 at 23:48
  • Thank you for changing your mind! Commented Nov 27, 2010 at 23:49
  • I'm not the one who gave it -1 in the first place... just asking that person to reconsider. Your question is not particularly badly written or unclear. Commented Nov 27, 2010 at 23:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I did. You still used File(); had you used JarFile() instead, you could have accessed the properties with JarEntry.toString(). In any case, you got it working with getResourceAsStream(), which is simple and reliable.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.