简体   繁体   English

在NetBeans 7.4中创建一个(便携式)jar文件,该文件能够找到目录的路径

[英]Create a (portable) jar file in NetBeans 7.4 able to find paths to directories

Back in the 2000s, I created a java package which I subdivided into the following 4 directories: 早在2000年代,我创建了一个Java程序包,将该程序包细分为以下4个目录:

  • A source code directory (*.java) 源代码目录(* .java)
  • An image directory (containing exclusively *.gif for my GUI) 图像目录(GUI中仅包含* .gif)
  • A parameter directory (containing text files, to which the package writes (output) and from which the package reads (input)) 参数目录(包含文本文件,程序包将其写入(输出),并且程序包将从其中读取(输入))
  • A database directory (containing data in text files which the package reads) 数据库目录(包含程序包读取的文本文件中的数据)

Back then, I created a *.jar with the source code directory (only). 那时,我创建了带有源代码目录(仅)的* .jar。 The jar was able to access the information in the three other directories, just by placing the 4 directories in the same folder (and specifying paths relative to the folder in the source code). 通过将4个目录放在同一文件夹中(并在源代码中指定相对于该文件夹的路径),该jar就能访问其他三个目录中的信息。 Importantly, I could zip up the folder and migrate it to another computer and have a fully working package. 重要的是,我可以压缩文件夹并将其迁移到另一台计算机上,并具有可以正常使用的软件包。 The source code in the jar uses such expressions as: jar中的源代码使用以下表达式:

  • ImageIcon A = new ImageIcon(..."A.gif"); ImageIcon A =新的ImageIcon(...“ A.gif”);
  • BufferedReader B = new BufferedReader(new FileReader(...."aGivenFileName")); BufferedReader B =新的BufferedReader(新的FileReader(....“ aGivenFileName”));

to access information in the 3 external directories. 访问3个外部目录中的信息。

Recently, I have created a project in NetBeans IDE 7.4 by importing my old package. 最近,我通过导入旧程序包在NetBeans IDE 7.4中创建了一个项目。 The new jar file finds the paths to the other 3 directories only if I hardwire the full paths (C:..) to the 3 directories in the source code. 仅当我硬连接源代码中3个目录的完整路径(C:..)时,新的jar文件才会找到其他3个目录的路径。 (Obviously, the package is no longer portable to other computers.) How can the new java 7.4 jar file find the relative paths to the 3 directories which would allow my package to be portable, again? (显然,该程序包不再可移植到其他计算机。)新的Java 7.4 jar文件如何找到3个目录的相对路径,这三个目录又使我的程序包可移植? (I have also tried bundling all four directories into the one jar file but that does not get me anywhere.) Thank you. (我也尝试将所有四个目录捆绑到一个jar文件中,但这并不能使我走到任何地方。)谢谢。

Netbeans has the final result (including everything) ready to be distributed in the dist subdirectory. Netbeans已准备好将最终结果(包括所有内容)分发到dist子目录中。 If you have special pathes you should include them in the manifest inside the Jar (see here ). 如果您有特殊的路径,则应将其包括在Jar的清单中(请参见此处 )。

If you want Netbeans to do that automatically, you can add those sub-directories as resources or libraries, so Netbeans will copy them automatically and set the manifest accordingly. 如果希望Netbeans自动执行此操作,则可以将这些子目录添加为资源或库,因此Netbeans将自动复制它们并相应地设置清单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM