87123

Jar file can't write files to local machine

Question:

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:

<ol><li>Look at data sheet</li> <li>Transpose data into text fields in GUI</li> <li>Click save and produce a text file containing this data in my selected directory</li> </ol>

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(); } } } } }

Answer1:

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 <a href="https://stackoverflow.com/questions/8302894/what-is-the-difference-between-runnable-jar-library-handling-options" rel="nofollow">here</a>, 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?

Recommend

  • VerifyInputWhenFocusTarget property has no effect
  • Implementing java gui login using multithreading
  • CUDA Debugging - VS on windows workstation, GPUs on Linux server?
  • dismiss Progress Dialog in another Activity … Android
  • How to remove all of a jQuery UI dialog when it is closed
  • MAVEN : Run Multiple Maven Project using Maven Test
  • How can I prevent the need to copy strings passed to a avr-gcc C++ constructor?
  • How to log in to an HTTPS website with Jsoup?
  • Why must we declare a variable name when adding a method to a struct in Golang?
  • Regarding starting the threads on a condition
  • Debugging VB6 Code From Visual Studio 2010
  • Diff between two dataframes in pandas
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Inline R code in YAML for rmarkdown doesn't run
  • Django simple Captcha “No module named fields” error
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • Keep this build forever option - Jenkins
  • Rails Find when some params will be blank
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Alert pop up with LWUIT
  • Recording logins for password protected directories
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Splitting given String into two variables - php
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Can Jackson SerializationFeature be overridden per field or class?
  • Controls, properties, events and timers running in design time
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Check if a string to interpolate provides expected placeholders
  • Updated Ionic CLI but shows previous version (Windows)
  • How to show dropdown in excel using jrxml (jasper api)?
  • jquery mobile loadPage not working
  • Data Validation Drop Down Box Arrow Disappearing
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How do you join a server to an Active Directory (domain)?
  • how does django model after text[] in postgresql [duplicate]
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?