Jar form file name java code

I would like to determine the jar file name from my java code. I found many solutions on google but nothing works. Just to see what I tried here, this is the stackoverflow forum, where a bunch of solutions are hosted : https://stackoverflow.com/a/167268/

I have Mac OS X 10.6.5.
When I enter 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 am editing my post to answer for a comment.
Some of the solutions give me a "null" value when I want the System.out.println path, and also fail when I want to create an instance of the file.
Other solutions, when I ask for a path that they don’t give, for example, file:/.....instead give something like rsch:/or something like this, I don’t know for sure, but this is a simple 4-character word.


Edit 2: I am running an executable jar from the console. And I would like to have this jar file name in the classes that are in the jar executable.


Edit 3:
4-digit word: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"));
    }

    }

Using 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 works fine in eclipse, but when I create the runnable jar file, I think you can see the problem.

+3
2

, , , :

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

, .

+1

, ? 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() .

+1

Source: https://habr.com/ru/post/1777241/


All Articles