Results 1 to 3 of 3
  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Trouble with runnable jar

    Big trouble in little china. Anyone know where to start, or how to track down these exceptions?

    Trouble with runnable jar-testanything.gif

    Here's the code. I'm just trying to open JFileChooser in a folder I am creating, or if the user wants to save files elsewhere I want to save that location in preferences so I can open the showSaveDialog there the next time. I haven't gotten to the code to save the user generated files yet. Trying to see if I can get this part working first.
    Java Code:
    import java.io.*;
    import java.util.prefs.Preferences;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    
    public class TestAnything {
    	final Preferences alphapref = Preferences.userRoot();
    
    	 
    	 public static void main(String []args) throws Exception{
    		 JFileChooser fc = new JFileChooser();
    		 FileNameExtensionFilter filter = new FileNameExtensionFilter(
    			        "AlphaPattern Custom Lesson Files", "aclf");
    		 fc.addChoosableFileFilter(filter);
    		 String prefPath = (new File(".").getCanonicalPath()
    				 + "\\Custom Lessons");
    		 String dummyFile = prefPath + "\\Save Your" +
    		 		" Lessons Here.not_a_file";
    
    		 boolean fExists = (new File(prefPath).exists());
    		 if (fExists){
    			 if (!new File(prefPath).isDirectory()){
    				 File f = new File(dummyFile);
    				 f.createNewFile();
    			 }
    		 }
    		 else {
    			 File f = new File(dummyFile);
    			 f.createNewFile();
    		 }
    		 boolean prefExists = Preferences.userRoot().nodeExists("/alphapref");
    		 if (!prefExists){
    			// This line will create a new node named "alphapref"
    			  Preferences.userRoot().node("/alphapref");
    			  System.out.println("alphapref created.");
    		 }
    		 Preferences pref = Preferences.userRoot().node("/alphapref");
    		 pref.flush();
    		 String nodir = "no directory";
    		 String key	= "last_dir";
    		 String checkForKey = KeyValue(key);
    		 if (checkForKey != null && checkForKey != key){
    			 pref.flush();
    			 String customPath = pref.get(key, null);
    			 System.out.println("Using saved preferences. path = " + customPath);
    			 File pathSetter = new File(customPath);
    			 fc.setCurrentDirectory(pathSetter);
    			 int returnVal = fc.showSaveDialog(null);
    			 if (returnVal == JFileChooser.APPROVE_OPTION) {
    				 File file = fc.getSelectedFile();
    			 }
    		 }
    		 
    		 if (checkForKey == null){
    			 pref.put(key, nodir);
    			 System.out.println("check for key failed. key and value" +
    		 		" added. checkForKey returned " + checkForKey);
    			 String customPath = dummyFile;
    			 System.out.println("customPath = " + customPath);
    			 File pathSetter = new File(customPath);
    			 fc.setCurrentDirectory(pathSetter);
    			 int returnVal = fc.showSaveDialog(null);
    			 if (returnVal == JFileChooser.APPROVE_OPTION) {
    				 File file = fc.getSelectedFile();
    
    			 }
    		 }
    		 else if (checkForKey == key){
    			 System.out.println("check for file returned \"no directory\"");
    			 String customPath = dummyFile;
    			 System.out.println("customPath = " + customPath);
    			 File pathSetter = new File(customPath);
    			 fc.setCurrentDirectory(pathSetter);
    			 int returnVal = fc.showSaveDialog(null);
    			 if (returnVal == JFileChooser.APPROVE_OPTION) {
    				 File file = fc.getSelectedFile();
    
    			 }
    		 }
    	}
                   private static String KeyValue(String value){
    		
    		Preferences node = Preferences.userRoot().node("/alphapref");
    		try {
    		String[] keys = node.keys();
    		for (int i=0; i<keys.length; i++) {
    			if (value.equals(keys[i])){ 
    				return keys[i];
    			}
    		}
    		} catch (Exception e) {
    		  
    		}
    		return null;
    		}
    		  
    	}
    No problems compiling or running it in Eclipse. I just wanted to see how this part would work when jarred up.

  2. #2
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Trouble with runnable jar

    Quote Originally Posted by NotTooOld View Post
    Big trouble in little china. Anyone know where to start, or how to track down these exceptions?

    No problems compiling or running it in Eclipse. Everything works as expected there. I just wanted to see how this part would work when jarred up.
    I went in and removed the "Throws Exception" and added try/catch where needed. The (runnable jar) class now opens the JFileChooser, but fails to create the folder and file I want it to. Latest Exceptions, and the new code are below. ( TestAnything.java:47 is actually the line that reads: "f.createNewFile();" ) All help is greatly appreciated.

    Trouble with runnable jar-errors1.gif

    Java Code:
    import java.io.*;
    import java.util.prefs.BackingStoreException;
    import java.util.prefs.Preferences;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    
    public class TestAnything {
    	final Preferences pref = Preferences.userRoot();
    
    	 
    	 public static void main(String []args){
    		 JFileChooser fc = new JFileChooser();
    		 FileNameExtensionFilter filter = new FileNameExtensionFilter(
    			        "AlphaPattern Custom Lesson Files", "aclf");
    		 fc.addChoosableFileFilter(filter);
    		 String prefPath = "";
    		try {
    			prefPath = (new File(".").getCanonicalPath()
    					 + "\\Custom Lessons");
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		 String dummyFile = prefPath + "\\Save Your" +
    		 		" Lessons Here.not_a_file";
    
    		 
    		 
    		 boolean fExists = (new File(prefPath).exists());
    		 if (fExists){
    			 if (!new File(prefPath).isDirectory()){
    				 File f = new File(dummyFile);
    				 try {
    					f.createNewFile();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			 }
    		 }
    		 else {
    			 File f = new File(dummyFile);
    			 try {
    				f.createNewFile();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		 }
    		 
    		try {
    			boolean prefExists = Preferences.userRoot().nodeExists("/alphapref");
    /*		 catch (BackingStoreException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}*/
    		 if (!prefExists){
    			// This line will create a new node named "alphapref"
    			  Preferences.userRoot().node("/alphapref");
    			  System.out.println("alphapref created.");
    		 }
    		 Preferences pref = Preferences.userRoot().node("/alphapref");
    		 pref.flush();
    		 String nodir = "no directory";
    		 String key	= "last_dir";
    		 String checkForKey = KeyValue(key);
    		 if (checkForKey != null && checkForKey != key){
    			 pref.flush();
    			 String customPath = pref.get(key, null);
    			 System.out.println("Using saved preferences. path = " + customPath);
    			 File pathSetter = new File(customPath);
    			 fc.setCurrentDirectory(pathSetter);
    			 int returnVal = fc.showSaveDialog(null);
    			 if (returnVal == JFileChooser.APPROVE_OPTION) {
    				 File file = fc.getSelectedFile();
    			 }
    		 }
    		 
    		 if (checkForKey == null){
    			 pref.put(key, nodir);
    			 System.out.println("check for key failed. key and value" +
    		 		" added. checkForKey returned " + checkForKey);
    			 String customPath = dummyFile;
    			 System.out.println("customPath = " + customPath);
    			 File pathSetter = new File(customPath);
    			 fc.setCurrentDirectory(pathSetter);
    			 int returnVal = fc.showSaveDialog(null);
    			 if (returnVal == JFileChooser.APPROVE_OPTION) {
    				 File file = fc.getSelectedFile();
    
    			 }
    		 }
    		 else if (checkForKey == key){
    			 System.out.println("check for file returned \"no directory\"");
    			 String customPath = dummyFile;
    			 System.out.println("customPath = " + customPath);
    			 File pathSetter = new File(customPath);
    			 fc.setCurrentDirectory(pathSetter);
    			 int returnVal = fc.showSaveDialog(null);
    			 if (returnVal == JFileChooser.APPROVE_OPTION) {
    				 File file = fc.getSelectedFile();
    
    			 }
    		 }
    		 
    		 
    	 	} catch (BackingStoreException e) {
    			e.printStackTrace();
    		}
    //		 else {
    			 
    
    //			 }
    //		 	pref.put(key, "C:\\Eclipse\\workspacezero\\TestPattern\\src\\PatternTest\\Custom Lessons\\Save Your Lessons Here.not_a_file");
    //			 pref.clear();
    //			 pref.flush();
    //			 pref.removeNode();
    	}
    	private static String KeyValue(String value){
    		
    		Preferences node = Preferences.userRoot().node("/alphapref");
    		try {
    		String[] keys = node.keys();
    		for (int i=0; i<keys.length; i++) {
    			if (value.equals(keys[i])){ 
    				return keys[i];
    			}
    		}
    		} catch (Exception e) {
    		  
    		}
    		return null;
    		
    		
    		}
    		  
    	}
    Last edited by NotTooOld; 05-02-2013 at 04:33 AM.

  3. #3
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Trouble with runnable jar

    too easy! Sorry to bother anyone here. A quick "google" of the exception pointed me to this code which fixed everything.
    Java Code:
    File folder = new File(prefPath);
    folder.mkdirs();
    folder.createNewFile();
    I think I'm still using "createNewFile()" incorrectly, but I'll now go change it to the proper ...
    Java Code:
    File folder = new File(prefPath);
    folder.mkdirs();
    boolean made = folder.createNewFile();
    Google first! Ask here if you really can't find it yourself. <--- talking to myself.

    Disclaimer: I tend to post questions at 02:00 or later, when my brain is already sleeping, but my stuborness is preventing my body from going to bed.
    Last edited by NotTooOld; 05-02-2013 at 07:38 AM.

Similar Threads

  1. Help with runnable jar
    By NotTooOld in forum New To Java
    Replies: 15
    Last Post: 04-17-2013, 01:22 PM
  2. How to use data from obtainMessage in Runnable?
    By iceman999 in forum Android
    Replies: 0
    Last Post: 02-06-2013, 06:54 PM
  3. debug runnable jar
    By Levian in forum New To Java
    Replies: 0
    Last Post: 08-11-2011, 06:38 AM
  4. Running runnable jar AS
    By sashaxiv in forum Eclipse
    Replies: 0
    Last Post: 03-16-2011, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •