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.
 
     
     
    