How to find out location from where the main java class you are running is loaded

You are here : Home / Core Java Tutorials /

In this tutorial we will learn program on How to find out location from where the main java class you are running is loaded. i.e. find out location from where the main class you are running is loaded.For Example - If your main class name is Abc then find out path of class Abc



Contents of page >
  • Program 1 - Write Program on How to find out location from where the main java class you are running is loaded >
  • Program 2 - Write Program on How to find out location from where the main java class you are running is loaded >


Program 1 - Write Program on How to find out location from where the main java class you are running is loaded >
We will find out the path of main java class which you are running using new File(FindPathOfMainJavaClassWhichYouAreRunning.class.getProtectionDomain().getCodeSource().getLocation().getPath())

package classPath;
import java.io.File;
import java.io.IOException;
/**
* Program on How to find out location from where the main java class you are running is loaded
*
*/
public class FindPathOfMainJavaClassWhichYouAreRunning {
   public static void main(String[] args) throws IOException {
         
       //how to find out the path of main java class which you are running.
       //i.e. find out location from where the main class you are running is loaded.
       //For Example - If your main class name is Abc then find out path of class Abc
          File file = new File(FindPathOfMainJavaClassWhichYouAreRunning.class
                       .getProtectionDomain().getCodeSource().getLocation().getPath());
          System.out.println("location from where the main java class you are running is loaded > "
                       + file.getAbsolutePath());
      
   }
}
/*OUTPUT
location from where the main java class you are running is loaded > E:\workspace\FileHandling\bin
*/
Output of program may vary on different systems depending location of java main class.



Program 2 - Write Program on How to find out location from where the main java class you are running is loaded >

We will find out the path of main java class which you are running using ClassLoader loader = MainClassName.class.getClassLoader();

package classPath;
import java.io.IOException;
/**
* Program on How to find out location from where the main java class you are running is loaded
*
*/
public class FindPathOfMainJavaClassWhichYouAreRunning2 {
   public static void main(String[] args) throws IOException {
       //how to find out the path of main java class which you are running.
       //i.e. find out location from where the main class you are running is loaded.
       //For Example - If your main class name is Abc then find out path of class Abc
          ClassLoader classLoader = FindPathOfMainJavaClassWhichYouAreRunning2.class
                       .getClassLoader();
         
          System.out.println("location from where the main java class "
                       + "you are running is loaded > "
                       + classLoader.getResource("classPath/FindPathOfMainJavaClassWhichYouAreRunning2.class"));
   }
}
/*OUTPUT
location from where the main java class you are running is loaded > file:/E:/workspace/FileHandling/bin/classPath/FindPathOfMainJavaClassWhichYouAreRunning2.class
*/
Output of program may vary on different systems depending location of java main class.


Summary -
So in this tutorial we learned how to find out location from where the main java class you are running is loaded in java.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

How to terminate JVM in java

Find out Java version (32 or 64 bit) installed in your system using java program and cmd

how to check string contains substring or not using indexOf and contains method in java


eEdit
Must read for you :