Michael Scott
Michael Scott

Reputation: 429

Creating a directory wherever jar file is located through java

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

Upvotes: 5

Views: 6044

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 4

Mateusz Kowalczyk
Mateusz Kowalczyk

Reputation: 2066

With some changes (such as adding a "/" before Comics), I managed to create the directory where you expected it to. Here is the full code I used.

import java.io.*;
public class TestClass {

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

        }
        public static void main(String[] argv) {
                try {
                        String d = getProgramPath() + "/Comics/";
                        System.out.println("Making directory at " + d);
                        File dir = new File(d);//The name of the directory to create                                                                                      
                        dir.mkdir();//Creates the directory                                                                                                               
                }
                catch (Exception e) { System.out.println("Exception occured" + e);}
        }
}

In the future, please don't hard code things like "/" and such. Use built-in libraries which will ask the OS what is right in this case. This ensures the functionality doesn't break (as easily) cross platform.

Of course, catch the exception properly etc. This is just quick and dirty attempt to mold your code into something that works.

Upvotes: 2

Related Questions