Jar文件无法将文件写入本地计算机 [英] Jar file can't write files to local machine
问题描述
我知道Java Applet对它们可以访问和不能访问的内容有很多限制,但是我认为JAR是不同的.作为记录,我是JAR文件之类的新手.
I know Java Applets have a bunch of restrictions as to what they can and cannot access, but I thought JARs were different. For the record, I'm new to JAR files and the like.
我已经为数据输入工具编写了GUI,并且我希望能够将格式化的数据写入文本文件.从Eclipse运行程序时,此方法有效.但是,一旦打包到JAR文件中,我将无法再执行写文件功能.它也不会出错.
I have written a GUI for a data entry tool, and I would like to be able to write the formatted data to a text file. This works when the program is run from Eclipse. However, once packaged in a JAR file, I can no longer get the write-to-file piece to work. It doesn't error out either.
这里没有共享的代码行,但是该程序基本上允许用户执行的操作是:
There's no single line of code to share here, but what the program basically allows the user to do is:
- 查看数据表
- 将数据转换为GUI中的文本字段
- 单击保存"并在我选择的目录中生成一个包含此数据的文本文件
最后一步是我可运行的JAR使我失败的地方.我单击保存",然后对话框出现,所有内容都出现了,但是当我检查输出文件夹时,没有文件产生.注意:这与错误的路径无关—我已经检查过了.就像我上面说的,它可以在Eclipse中工作.
That last step is where my runnable JAR is failing me. I click save and my dialog box shows up and everything, but when I check the output folder, no file is produced. NOTE: It does not have to do with an incorrect path--I've checked that. As I said above, it works in Eclipse.
我缺少一些限制吗?理想情况下,我将能够与其他一些人共享该可执行文件,以分担数据输入任务.是否有其他格式可以打包该程序以使其正常工作? (我花了很长时间在此上,并希望使其能够在Java中工作)
Is there some restriction I'm missing? Ideally I'd be able to share this executable file with a few others to divvy up the data entry task. Is there another format for packaging this program that will allow it to work? (I've spend a long time on this and would like to make it work in Java)
下面是保存按钮的我的ActionListener.这应该是魔术发生的地方:
Below is my ActionListener for the save button. This is where the magic should be happening:
class WriteToFileListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (command.equals(strWriteToFile)){
System.out.println(strWriteToFile);
int warning = JOptionPane.showOptionDialog(frame, "Text",
"Text", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
if (warning == 0)
{
Path path = Paths.get(tfOutputDir.getText() + "\\" + tfFileName.getText() + ".txt");
ArrayList<String> output = new ArrayList<String>();
for (String s : tmp.items)
{
output.add(s);
}
try {
Files.write(path, output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
推荐答案
好吧,我认为我确实对自己的问题有一个答案,但我并不十分了解为什么:
Okay I think I actually have an answer to my own question, but I don't really understand why:
在Eclipse中打包JAR文件时,我选择了将所需的库打包到生成的JAR中".那给了我个问题.
When packaging the JAR file in Eclipse, I had chosen "Package required libraries into generated JAR." That gave me the problem.
在再次尝试时,我选择了将所需的库提取到生成的JAR中".那解决了问题.
In trying again, I chose "Extract required libraries into generated JAR." That solved the problem.
There's a good explanation of the difference between the two options here, but I don't understand why this affected my ability to save. Perhaps @m0skit0 had a point about the path? Could it have been trying to write in the JAR?
这篇关于Jar文件无法将文件写入本地计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!