5

我已经对 SO 进行了调查以寻求答案,但找不到合适的答案。

当我从 jar 启动程序时,我需要在 jar 文件所在的目录中创建一个文件夹。用户将 jar 文件保存在哪里并不重要。

这是我正在使用的最新代码: ASystem.out.println将打印出正确的目录,但不会创建文件夹。相反,到目前为止,所有内容都保存到我的 System32 文件夹中。

    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
4

2 回答 2

4

获取 Jar 的路径可能比简单地获取 user.dir 目录要复杂一些。我不记得详细原因,但 user.dir 在所有情况下都不会可靠地返回此路径。如果您绝对必须获取 jar 的路径,那么您需要做一些黑魔法并首先获取类的 protectionDomain。就像是:

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;
   }
}

即使这样也不能保证有效,您必须接受这样一个事实,即有时(例如出于安全原因)您将无法获得 Jar 的路径。

于 2012-06-23T04:34:22.967 回答
2

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.

于 2012-06-23T03:56:42.427 回答