How to get the path of running java program

Java

Java Problem Overview


Is there a way to get the path of main class of the running java program.

structure is

D:/
|---Project
       |------bin
       |------src

I want to get the path as D:\Project\bin\.

I tried System.getProperty("java.class.path"); but the problem is, if I run like

java -classpath D:\Project\bin;D:\Project\src\  Main

Output 
Getting : D:\Project\bin;D:\Project\src\
Want    : D:\Project\bin

Is there any way to do this?



===== EDIT =====

Got the solution here

Solution 1 (By Jon Skeet)

package foo;

public class Test
{
    public static void main(String[] args)
    {
        ClassLoader loader = Test.class.getClassLoader();
        System.out.println(loader.getResource("foo/Test.class"));
    }
}

This printed out:

file:/C:/Users/Jon/Test/foo/Test.class


Solution 2 (By Erickson)

URL main = Main.class.getResource("Main.class");
if (!"file".equalsIgnoreCase(main.getProtocol()))
  throw new IllegalStateException("Main class is not stored in a file.");
File path = new File(main.getPath());

Note that most class files are assembled into JAR files so this won't work in every case (hence the IllegalStateException). However, you can locate the JAR that contains the class with this technique, and you can get the content of the class file by substituting a call to getResourceAsStream() in place of getResource(), and that will work whether the class is on the file system or in a JAR.

Java Solutions


Solution 1 - Java

Use

System.getProperty("java.class.path")

see http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

You can also split it into it's elements easily

String classpath = System.getProperty("java.class.path");
String[] classpathEntries = classpath.split(File.pathSeparator);

Solution 2 - Java

Try this code:

final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());

replace 'MyClass' with your class containing the main method.

Alternatively you can also use

System.getProperty("java.class.path")

Above mentioned System property provides

> Path used to find directories and JAR archives containing class files. > Elements of the class path are separated by a platform-specific > character specified in the path.separator property.

Solution 3 - Java

You actually do not want to get the path to your main class. According to your example you want to get the current working directory, i.e. directory where your program started. In this case you can just say new File(".").getAbsolutePath()

Solution 4 - Java

    ClassLoader cl = ClassLoader.getSystemClassLoader();

    URL[] urls = ((URLClassLoader)cl).getURLs();

    for(URL url: urls){
    	System.out.println(url.getFile());
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenim DattaView Question on Stackoverflow
Solution 1 - JavaRené LinkView Answer on Stackoverflow
Solution 2 - JavaJuned AhsanView Answer on Stackoverflow
Solution 3 - JavaAlexRView Answer on Stackoverflow
Solution 4 - JavaGanesh RengarajanView Answer on Stackoverflow