Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8129855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:27:45+00:00 2026-06-06T08:27:45+00:00

I have already surveyed SO for an answer, and could not find an appropriate

  • 0

I have already surveyed SO for an answer, and could not find an appropriate one.

When I launch my program from a jar I need to create a folder in the directory where the jar file is located. It should not matter where the user saves the jar file.

Here is the newest code I was playing with: A System.out.println will print out the correct directory but the folder will not be created. In contrast,everything is being saved to my System32 folder as of now.

    public static String getProgramPath() throws IOException{
    String currentdir = System.getProperty("user.dir");
    currentdir = currentdir.replace( "\\", "/" );
    return currentdir;

}

File dir = new File(getProgramPath() + "Comics/");//The name of the directory to create
    dir.mkdir();//Creates the directory
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:27:47+00:00Added an answer on June 6, 2026 at 8:27 am

    To get a Jar’s path can be a little trickier than simply getting the user.dir directory. I can’t remember the details why, but user.dir does not return this path reliably in all situations. If you absolutely must get the jar’s path, then you need to do a little black magic and first get the class’s protectionDomain. Something like:

    import java.io.File;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URL;
    import java.net.URLDecoder;
    
    import javax.swing.JOptionPane;
    
    public class MkDirForMe {
       public static void main(String[] args) {
          try {
             String path = getProgramPath2();
    
             String fileSeparator = System.getProperty("file.separator");
             String newDir = path + fileSeparator + "newDir2" + fileSeparator;
             JOptionPane.showMessageDialog(null, newDir);
    
             File file = new File(newDir);
             file.mkdir();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    
       public static String getProgramPath2() throws UnsupportedEncodingException {
          URL url = MkDirForMe.class.getProtectionDomain().getCodeSource().getLocation();
          String jarPath = URLDecoder.decode(url.getFile(), "UTF-8");
          String parentPath = new File(jarPath).getParentFile().getPath();
          return parentPath;
       }
    }
    

    Even this isn’t guaranteed to work, and you’ll have to resign yourself to the fact that there are just some times (for instance for security reasons) when you won’t be able to get a Jar’s path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have already asked this question on asp.net forum, could not find any satisfactory
I have already checked this question but could not find what I'm looking for.
I have already cut everything I could from the main loop. I also optimized
I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate
I have already distributed the static content, so I do not need Apache to
I have already googled and searched stackoverflow but I can find nothing related to
I have already read some posts, but no one helped me with my problem.
I have already spent a lot of time on Google, but have not found
I have already can execute *.java or *.jar on PHP at my xampp, with
I have already created a report using list of data from my database. i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.