0

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.

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:

  1. Look at data sheet
  2. Transpose data into text fields in GUI
  3. Click save and produce a text file containing this data in my selected directory

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.

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)

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();
                }
            }
        }
    }
}
marcman
  • 3,233
  • 4
  • 36
  • 71
  • 2
    No, there are no restrictions. If you detail what do you mean by *"failing me"* we can help further. – m0skit0 Mar 16 '15 at 19:58
  • @m0skit0 Thanks--I elaborated in my question edits just now – marcman Mar 16 '15 at 20:00
  • What method was eclipse using to write to a file? Were you doing I/O Redirection? FileWriter? Serialization? – Aify Mar 16 '15 at 20:01
  • @Aify java.nio.file.Files and java.nio.file.Path – marcman Mar 16 '15 at 20:02
  • Your should debug and check what's the full path for the file. Eclipse has a different running environment, so sometimes paths are different. I know you "checked" it, but check again the real value for the file you're writing when running. – m0skit0 Mar 16 '15 at 20:05
  • I really don't think it's a path issue @m0skit0. I checked with a dialog box in the JAR. Basically I made it pop up with the path when I went to save. It was showing the right destination. – marcman Mar 16 '15 at 20:07
  • If you add a catch block for a `SecurityException`, does it catch anything when run as a JAR? Also, consider writing `Files.size(path)` to stdout in the `try` block after your `Files.write(..)`. If you get a non-zero output, you know writing the file completed, the file should exist (somewhere), and have a non-zero size--and otherwise, there may be some other error. – Eric Hughes Mar 16 '15 at 20:18

1 Answers1

2

Okay I think I actually have an answer to my own question, but I don't really understand why:

When packaging the JAR file in Eclipse, I had chosen "Package required libraries into generated JAR." That gave me the problem.

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?

Community
  • 1
  • 1
marcman
  • 3,233
  • 4
  • 36
  • 71