Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: locating the path to a jar file within a program

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default locating the path to a jar file within a program

    hello, i am writing a program which will be turned into an executable .jar file. i need to locate the directory in which the jar file is located, using code within the jar file. my code works only in the directory where my source code is located (not the .class files, the .java files).

    public class Example{
            public static void main(String[] args){
                    try{
                    	String exampleClassDirectory = Example.class.getResource("Example.class").getPath();
                    	File jarFileDirectory = new File(exampleClassDirectory).getParentFile();
                    	String containingDirectory = jarFileDirectory.getParent();
                    	JOptionPane.showMessageDialog(new JFrame(), containingDirectory,"Found jar file directory.",JOptionPane.PLAIN_MESSAGE);
                    }
                    catch(Exception e){
    	                JOptionPane.showMessageDialog(new JFrame(), e.toString(),"error",JOptionPane.ERROR_MESSAGE);
                    }
            }
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: locating the path to a jar file within a program

    Put the Jar file in the same area as your code



    You can find it under "Properties" if you're using Eclipse:


    Path: /wenguin/src/McWoo/Testing.java
    Type: File (Java Source File)
    Location: C:\Users\Paul\workspace\wenguin\src\McWoo\Testing. java
    Last edited by javapenguin; January 2nd, 2011 at 03:43 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: locating the path to a jar file within a program

    Wait, is the code supposed to make it a jar file for you? If not, just go to export and Runnable Jar File. If that won't work, try export and Jar File.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: locating the path to a jar file within a program

    Quote Originally Posted by javapenguin View Post
    Put the Jar file in the same area as your code



    You can find it under "Properties" if you're using Eclipse:


    Path: /wenguin/src/McWoo/Testing.java
    Type: File (Java Source File)
    Location: C:\Users\Paul\workspace\wenguin\src\McWoo\Testing. java

    this will be distributed onto other computers, where i won't know where the jar file is. that's why i'm trying to make code that locates the jar's path for me, so i don't have to tell the user to put the jar file in a specific directory.


    Quote Originally Posted by javapenguin View Post
    Wait, is the code supposed to make it a jar file for you? If not, just go to export and Runnable Jar File. If that won't work, try export and Jar File.
    No, i am not trying to make a jar file. i can do that myself. i am looking to locate the path of the jar file from within the program. i.e. there's an executable named Example.jar which i made. within Example.jar, there is Example.class. there is code inside Example.class which locates the directory containing Example.jar. that is the goal.
    Last edited by nemo; January 2nd, 2011 at 03:56 PM.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,228
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: locating the path to a jar file within a program

    Create a new File, which will create a file in the directory of the jar, then get the path from there
    File file = new File("temp");
    String path =file.getParentFile().getAbsolutePath();

  6. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (January 2nd, 2011)

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: locating the path to a jar file within a program

    Hmmmm...well,

    File aFile = new File();

    String absolutePathName = aFile.getAbsolutePath();

    String pathName = aFile.getPath();

    Also, maybe I'm wrong, but don't you need the full path anyway, not just the .class or .java part?

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: locating the path to a jar file within a program

    Quote Originally Posted by copeg View Post
    Create a new File, which will create a file in the directory of the jar, then get the path from there
    File file = new File("temp");
    String path =file.getParentFile().getAbsolutePath();
    That's the problem. How will it know where the directory is if the jar is installed or put anywhere on other computers?

    Also, why are you returning the path of the parent file? Why not the jar file itself?

    ---Edit---

    Oh the class is inside the jar file. Ok. Never mind then.
    Last edited by javapenguin; January 2nd, 2011 at 04:38 PM. Reason: Never mind.

  9. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: locating the path to a jar file within a program

    String exampleClassDirectory = Example.class.getResource("Example.class").getPath ();

    Also, it appears you're using a static variable called class to call getResource()

Similar Threads

  1. Making computer independent file path
    By Javabeginner in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 2nd, 2010, 03:56 PM
  2. Shared library path
    By sateesh.b in forum Java Native Interface
    Replies: 3
    Last Post: May 13th, 2010, 11:12 PM
  3. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  4. how to get full path name from realtive path
    By priyanka3006 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: August 10th, 2009, 04:28 AM
  5. [SOLVED] How to a set java class path?
    By captjade in forum Java Theory & Questions
    Replies: 1
    Last Post: March 10th, 2009, 06:40 AM