我想从我的jar中读取资源,如下所示:
File file;
file = new File(getClass().getResource("/file.txt").toURI());
BufferredReader reader = new BufferedReader(new FileReader(file));
//Read the file
Run Code Online (Sandbox Code Playgroud)
并且它在Eclipse中运行时工作正常,但是如果我将它导出到jar中运行它就会出现IllegalArgumentException:
Exception in thread "Thread-2"
java.lang.IllegalArgumentException: URI is not hierarchical
Run Code Online (Sandbox Code Playgroud)
而且我真的不知道为什么,但经过一些测试我发现如果我改变了
file = new File(getClass().getResource("/file.txt").toURI());
Run Code Online (Sandbox Code Playgroud)
至
file = new File(getClass().getResource("/folder/file.txt").toURI());
Run Code Online (Sandbox Code Playgroud)
然后它的工作正好相反(它适用于jar而不是eclipse).
我正在使用Eclipse,我的文件夹在一个类文件夹中.
可能重复:
如何从Java jar文件中读取资源文件?
谷歌搜索几个小时后开始完全疯狂.我也看到了网站上的问题的变化,但似乎无法让它工作.JFrame需要从ini文件中读取数据,并且我已经创建了一个打开所述文件的方法.所述文件存储在jar文件中名为resources的文件夹中.
private BufferedReader openIniFile(String filename){
BufferedReader brReader = null;
try{
File fileObj = new File(this.getClass().getResource("/resources/" + filename).toURI()); // The fileobject of the file to read
if(fileObj.exists())
brReader = new BufferedReader(new FileReader(fileObj));
} catch(Exception e){
System.err.println("Exception while opening file: " + e.getMessage());
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
当我在编译后运行代码时,这当然很有效,但在导出到.jar文件后抛出异常.我已经研究过使用InputStream,FileInputStream但似乎无法找到可行的解决方案.
如何让jar文件识别资源?
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("com/x/y/z.cfg");
File file = new File(url.getPath());
Run Code Online (Sandbox Code Playgroud)
这在从Eclipse运行jar文件时起作用,但在jar文件中运行时不起作用.
java.io.FileNotFoundException:file:\ C:\ Users \nova\Desktop\Matcher.jar!\ c om\x\y\z.cfg
这不是重复的.我检查了所有其他问题,没有有用的信息.
可能重复:
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对象.
我该如何解决这个问题?
谢谢,
马丁
我需要/test/a.xml从test.jar文件中读取内容(它们都是变量,当然,不是常量).最简单的方法是什么?
File file = new File("test.jar");
String path = "/test/a.xml";
String content = // ... how?
Run Code Online (Sandbox Code Playgroud) 由于基于此定义的极大查找表,我遇到了这个编译器错误:
//92 X 182 array
private static final double[][] lookUpTable = new double[][]
{
{ numbers....}
};
Run Code Online (Sandbox Code Playgroud)
据我所知,将其分解是一种解决方案,但要准确地分割这个阵列是非常困难的.我也相信我可以把它移到一个文件,但我不知道我是否可以用一种方式来帮助我格式化,而且我不想每秒都读取文件.还有其他建议可以帮助我解决这个问题吗?