【问题标题】:Creating a directory wherever jar file is located through java通过java创建jar文件所在的目录
【发布时间】:2012-06-25 08:51:32
【问题描述】:

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

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

这是我正在使用的最新代码:System.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

【问题讨论】:

  • 为什么要用 / 代替 \\?
  • 我试图找出一种创建目录的方法。那是用来用 / 替换 \ 的,因此路径可以正确转换为 Windows。我就是什么都做不了:/
  • user.dir 是你的主目录,它是固定的,完全不受你的 jar 所在位置的影响。并且您需要在目录名称(不包含尾部斜杠,它只是目录之间的分隔符)和“漫画”之间添加一个“/”。巧合的是,您的 user.dir 可以是 jar 所在的目录。使用此目录写入它也不是最好的主意:通常不允许用户写入安装程序的目录。

标签: java path directory


【解决方案1】:

获取 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 的路径。

【讨论】:

    【解决方案2】:

    通过一些更改(例如在 Comics 前添加“/”),我设法创建了您期望的目录。这是我使用的完整代码。

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

    以后,请不要硬编码“/”之类的东西。使用内置库,它会询问操作系统在这种情况下什么是正确的。这确保了该功能不会(很容易地)跨平台破坏。

    当然,正确捕获异常等等。这只是快速而肮脏地尝试将您的代码塑造成有效的东西。

    【讨论】:

    • 如果用户以java -jar /foo/bar/some.jar 运行JAR 并且他当前没有/foo/bar 作为他的当前目录,这将不起作用。
    • 由于某种原因,当我尝试将代码与我的程序一起使用时,它不起作用!它确实适用于单个测试类文件。很奇怪……
    猜你喜欢
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2011-08-09
    • 2011-06-03
    • 1970-01-01
    • 2017-05-15
    相关资源
    最近更新 更多