Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By gimbal2

Thread: How to get a file from the same folder as the jar being run?

  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default How to get a file from the same folder as the jar being run?

    Hi all,

    I am creating a bukkit plugin, but my question has nothing to do with bukkit, just Java. I am trying to load a file that is located in the same folder as the jar file that is being run and I am having no luck. I can't set a manual path because if I move this plugin to a remote server it won't work. So if anyone knows how to get a file from the same folder as the jar could they help me out?

    Thank you all very much for your time.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: How to get a file from the same folder as the jar being run?

    Please elaborate on:
    1. the way application is started (e.g., via command line, script, double-clicking on desktop icon, etc.)
    2. the directory from which the application is started
    3. the location of the jar file relative to the startup directory (from #2)

    The answer to your question can be as simple as System.getProperty("user.dir"), or something more elaborate depending on the answers for the above...

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: How to get a file from the same folder as the jar being run?

    There are ways to get the path to the jar file an executing class is in. I forget what the technique is right now.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: How to get a file from the same folder as the jar being run?

    Quote Originally Posted by jashburn View Post
    Please elaborate on:
    1. the way application is started (e.g., via command line, script, double-clicking on desktop icon, etc.)
    2. the directory from which the application is started
    3. the location of the jar file relative to the startup directory (from #2)

    The answer to your question can be as simple as System.getProperty("user.dir"), or something more elaborate depending on the answers for the above...
    1. I'm assuming that the application is started via command line, because Bukkit does that.
    2. The directory for the jar is C:/Users/Patrick/Dekstop/Server/plugins/InventoryMenu.jar, but I can't use that because once I release this plugin, it will have to be different for everyone's computers.
    3. The jar file is just inside of the folder.

    What I need to find out is just how to get the path to the folder, but not by me manually typing it in.

    Thanks.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default Re: How to get a file from the same folder as the jar being run?

    Quote Originally Posted by Norm View Post
    There are ways to get the path to the jar file an executing class is in. I forget what the technique is right now.
    Epic Google: "java executable jar find path to self"
    Result: java - How to get the path of a running JAR file? - Stack Overflow

    Quote:

    Java Code:
    return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    kjkrum likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: How to get a file from the same folder as the jar being run?

    Quote Originally Posted by gimbal2 View Post
    Epic Google: "java executable jar find path to self"
    Result: java - How to get the path of a running JAR file? - Stack Overflow

    Quote:

    Java Code:
    return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    Thanks!

Similar Threads

  1. jar file in temporary folder
    By Boule in forum Java Applets
    Replies: 2
    Last Post: 10-25-2012, 10:39 AM
  2. Replies: 0
    Last Post: 12-21-2011, 10:15 AM
  3. Replies: 0
    Last Post: 11-12-2011, 02:26 PM
  4. Writing to a file in a different folder
    By tiemykim in forum New To Java
    Replies: 1
    Last Post: 03-14-2010, 06:58 AM
  5. need to get the file name from folder
    By naveen.dpt2007 in forum New To Java
    Replies: 2
    Last Post: 09-04-2009, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •