1

The jar file is residing in the Linux file system directory

/home/user/hello.jar

Whenever the hello.jar is run I am getting runtime environment path not the physical path where jar is residing, need to get the physical location of the jar i.e

/home/user

How can I do it?

syto
  • 65
  • 2
  • 8
  • I used following code to obtain location of class: `URL location = msg.getClass().getResource('/'+ msg.getClass().getName().replace('.', '/')+".class"); ` Location contains full path to jar and path to class in jar – rumis Apr 15 '15 at 06:11
  • It works for windows, not for Linux. I am getting runtime execution path, not the physical locn of jar – syto Apr 15 '15 at 06:12
  • 1
    Why do you need it? If you need resources from the jar you can just do `MainClassName.getClass().getResourceAsStream("/path/in/jar/to/file")` That way it doesnt matter where your jar is located. To always be sure where your jar is make an installer and have an install folder for external resources. like `AppData/somefolder/.applicationname` and under unix flavor `$home/.applicationname` – Tschallacka Apr 15 '15 at 06:13
  • 1
    Have you tried http://stackoverflow.com/questions/11747833/getting-filesystem-path-of-class-being-executed – Tschallacka Apr 15 '15 at 06:17
  • @RC. I think it's not a duplicate. There is a difference between current working directory and the directory, where a jar resides. – ivant Apr 15 '15 at 06:17
  • Yes I tried, but not getting the physical locn. This is what I am getting /usr/hdp/2.2.0.0-2041/hadoop/conf:/usr/hdp/2.2.0.0-2041/hadoop/lib/jars – syto Apr 15 '15 at 06:24
  • Again, why? There's no guarantee that a class is in a jar or that a jar has a "physical location", as it could be embedded somewhere. – chrylis -cautiouslyoptimistic- Apr 15 '15 at 13:00
  • I want to read the properties file externally where it is residing with jar in a directory – syto Apr 15 '15 at 14:45

3 Answers3

1
new File( mainClass.getProtectionDomain()
                   .getCodeSource()
                   .getLocation().toURI() )

should do the trick, assuming mainClass is a java.lang.Class in your jar file.

edit:

complete main class:

import java.io.File;

public class App
{
    public static void main( String[] args ) throws Exception
    {
        Class<App> mainClass = App.class;
        File f= new File( mainClass.getProtectionDomain()
                           .getCodeSource()
                           .getLocation().toURI() );
        System.out.println(f);
    }
}
Max Fichtelmann
  • 3,366
  • 1
  • 22
  • 27
0

I don't think there is a very reliable way to get this. The best I can think of is to parse the classpath and work your way from there. Here is how to get get the classpath elements:

    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    URL[] urls = ((URLClassLoader) classLoader).getURLs();

This code assumes that we're using the default system class loader.

ivant
  • 3,909
  • 1
  • 25
  • 39
  • I am not getting the physical locn /home/user, what I am getting after running the above code is /usr/hdp/2.2.0.0-2041/hadoop/conf:/usr/hdp/2.2.0.0-2041/hadoop/lib/jars – syto Apr 15 '15 at 06:41
  • The idea is to find the URL corresponding to your jar and extract the path from it. – ivant Apr 15 '15 at 06:46
0

Try this:

URL location = MyClassInJar.class.getResource('/' + MyClassInJar.class.getName().replace('.', '/') + ".class");
String s = location.toString();

This code return path to jar file and path to class in jar. For example

jar:file:/Oracle/Middleware/Oracle_Home/oracle_common/modules/com.sun.xml.ws.jaxws-rt_2.2.jar!/com/sun/xml/ws/api/message/MyClassInJar.class

So you can trim class name (full qualified name).

rumis
  • 197
  • 1
  • 6