在尝试使用我的java应用程序将jar文件中的某些文件复制到临时目录时,会抛出以下异常:
java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Unknown Source)
at com.sora.util.walltoggle.pro.WebViewPresentation.setupTempFiles(WebViewPresentation.java:83)
....
Run Code Online (Sandbox Code Playgroud)
这是我的一小部分setupTempFiles(带行号):
81. URI uri = getClass().getResource("/webViewPresentation").toURI();
//prints: URI->jar:file:/C:/Users/Tom/Dropbox/WallTogglePro.jar!/webViewPresentation
82. System.out.println("URI->" + uri );
83. Path source = Paths.get(uri);
Run Code Online (Sandbox Code Playgroud)
该webViewPresentation目录位于我的jar的根目录中:

当我将我的应用程序打包为jar时,此问题才会退出,在Eclipse中进行调试没有问题.我怀疑这与这个bug有关,但我不确定如何纠正这个问题.
任何帮助赞赏
如果事项:
我在Java 8 build 1.8.0-b132上
Windows 7 Ult.64位
我知道我们可以这样做:
Class.class.getResourceAsStream("/com/youcompany/yourapp/module/someresource.conf")
Run Code Online (Sandbox Code Playgroud)
读取我们的jar文件中打包的文件.
我已经google了很多,我肯定没有使用正确的条款; 我想要做的是列出可用的资源,如下所示:
Class.class.listResources("/com/yourcompany/yourapp")
Run Code Online (Sandbox Code Playgroud)
这应该返回包内的资源列表 com.yourcompany.yourapp.*
那可能吗?关于如何做到这一点的任何想法,以防它不能像我展示的那样轻松完成?
注意:我知道可以知道罐子的位置,然后打开它并检查其内容以实现它.但是,我不能在我现在工作的环境中这样做.
我试图做的是在程序的JAR中存储一个文本文件(不会改变),以便可以读取它.文本文件的目的是它将被我的一个类读入,文本文件的内容将被添加到JEditorPane.该文件基本上是一个教程,当用户点击选项阅读教程时,文件内容将被读取并显示在弹出的新窗口中.
我有它的GUI部分,但只要将文件存储在JAR中以便可以访问它,我就迷路了.我已经读过使用一个InputStream遗嘱,但在尝试了一些事情之后我还没有让它工作.
我还将图像存储在JAR中,以用作GUI窗口的图标.这完成了:
private Image icon = new ImageIcon(getClass()
.getResource("resources/cricket.jpg")).getImage();
Run Code Online (Sandbox Code Playgroud)
但是,这在尝试获取文件时不起作用:
private File file = new File(getClass.getResource("resources/howto.txt"));
Run Code Online (Sandbox Code Playgroud)
这是我现在的班级:
public class HowToScreen extends JFrame{
/**
*
*/
private static final long serialVersionUID = -3760362453964229085L;
private JEditorPane howtoScreen = new JEditorPane("text/html", "");
private Image icon = new ImageIcon(getClass().getResource("resources/cricket.jpg")).getImage();
private BufferedReader txtReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/howto.txt")));
public HowToScreen(){
setSize(400,300);
setLocation(500,200);
setTitle("Daily Text Tutorial");
setIconImage(icon);
howtoScreen.setEditable(false);
howtoScreen.setText(importFileStream());
add(howtoScreen);
setVisible(true);
}
public String importFile(){
String text = "";
File file = …Run Code Online (Sandbox Code Playgroud) 我必须读取包含字符串列表的文件.我试图按照这篇文章中的建议.两种解决方案都需要使用FileUtils.readLines,但使用a String而不是a File作为参数.
Set<String> lines = new HashSet<String>(FileUtils.readLines("foo.txt"));
Run Code Online (Sandbox Code Playgroud)
我需要一个File.
这篇文章 将是我的问题,除了OP被完全禁止使用文件.如果我想使用Apache方法,我需要一个文件,这是我最初的问题的首选解决方案.
我的文件很小(一百行左右)和每个程序实例的单例,所以我不需要担心在内存中有另一个文件副本.因此,我可以使用更基本的方法来读取文件,但到目前为止它看起来FileUtils.readLines可能更清晰.我如何从资源到文件.
可能重复:
Java资源作为文件
我是Java的新手,我试图在Jar文件中获取一个文本文件.
在我执行我的jar时,我必须将我的文本文件放在与jar文件相同的文件夹中.如果文本文件不存在,我会得到一个NullPointerException,我想避免.
我想要做的是在jar中获取txt文件,所以我不会遇到这个问题.我尝试了一些指南,但它们似乎没有用.我目前的读取功能如下:
public static HashSet<String> readDictionary()
{
HashSet<String> toRet = new HashSet<>();
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Dictionary.txt");
try (DataInputStream in = new DataInputStream(fstream)) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Read Lines
toRet.add(strLine);
}
}
return toRet;
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: …Run Code Online (Sandbox Code Playgroud) 我试图在我的runnable .jar文件中包含一些文本文件作为资源.以下代码应该打印一个文件的内容:
URI resourceFile = Driver.class.getResource("/etc/inputfile.txt").toURI();
System.out.println("Opening URI: " + resourceFile.toString());
File infile = new File(resourceFile);
Scanner sc = new Scanner(infile);
while (sc.hasNextLine())
System.out.println(sc.nextLine());
sc.close();
Run Code Online (Sandbox Code Playgroud)
导出为可运行的jar后,运行时得到以下输出:
打开URI:rsrc:etc/inputfile.txt
java.lang.IllegalArgumentException:URI在java.io.File中不是分层的.(File.java:363)
如果我使用getResourceAsStream,可以毫无问题地打印文件,但出于其他原因我想使用File对象.
我该如何解决这个问题?
谢谢,
马丁