This discussion is archived
7 Replies Latest reply: Jul 25, 2010 8:27 AM by 843804 RSS

Executable jar won't execute

843804 Newbie
Currently Being Moderated
So I wrote a board game program as an application, and I wanted to turn it into a standalone executable. To do this, I used the "Create Jar" function of my IDE (Jcreator), since I'm not completely sure how to add java to the classpath (a little help on that would be appreciated too. I'm running Windows Vista), but it seems to work fine. The first time I ran it (by double clicking), the jar executed, but it didn't run properly because my code referenced the location of the images directly. However, it still opened up the window of the game. Then I went back to my code to change all the image references to include "getResource". Then when I tried running it again, it still didn't run, but opened up the window. Then I figured out that this was because my code referenced some text files too, and I fixed this. However, this time, when I try making a jar and executing it, nothing happens! No window opens up at all. I went back and changed the code back to before I modified it for the text files, but the resulting jar still won't execute. I also tested the jar function on a different program of mine, and it works perfectly. Does anyone know what could be the problem?
  • 1. Re: Executable jar won't execute
    843804 Newbie
    Currently Being Moderated
    Run your application through console using 'java -jar yourApp.jar'.

    Reading the output it will be easier to detect the problem. If necessary post it here latter.
  • 2. Re: Executable jar won't execute
    843804 Newbie
    Currently Being Moderated
    Thanks so much! Now I have an error:
    Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
    I used this: File file = new File((getClass().getResource("blahblah.txt")).toURI()); in order to grab the text file from with the jar. How can I fix this?
  • 3. Re: Executable jar won't execute
    843804 Newbie
    Currently Being Moderated
    You have to use getResourceAsStream() to read the txt file inside your .jar application.
    Here follows an example:
    public class Test {
    
    public static void trying(){
              InputStream is = null;
                 BufferedReader br = null;
                 String line;
    
             try{
              
              is = Test.class.getResourceAsStream("/test.txt");
                 br = new BufferedReader(new InputStreamReader(is));
                 line = br.readLine();
                 System.out.println(line);
             }catch(IOException e){}
            
              
         }
         
         
         
         public static void main(String[] args) {
              // TODO Auto-generated method stub
    
              trying();
              
              
         }
    
    
    
    
    
    
    }
    If you have a test.txt file inside your .jar application, it will read the text and show it in the console
  • 4. Re: Executable jar won't execute
    843804 Newbie
    Currently Being Moderated
    That worked, thanks! Unfortunately, that doesn't seem to be the end of my problems... Apparently, I didn't fix the image references properly. I used:
    Image image = (new ImageIcon(getClass().getResource("images/image.gif"))).getImage();
    However, now I get a NullPointerException. The program runs fine when it's compiled and run normally though. How can I correct this?
  • 5. Re: Executable jar won't execute
    843804 Newbie
    Currently Being Moderated
    Wait a second... I used this same approach on one of my other programs, and it works fine. The only difference is that in the previous program, I put the gifs in the same directory as the class files, rather than in an "image" directory.
  • 6. Re: Executable jar won't execute
    843804 Newbie
    Currently Being Moderated
    Okay, nvm, I figured it out. Apparently, when you want to make a jar, everything becomes case sensitive. Thank you so much, ThomYork!!!
  • 7. Re: Executable jar won't execute
    843804 Newbie
    Currently Being Moderated
    I had the same problem. I renamed my Icons folder to icons halfway through. So some had the referens /Icons/example.jpg and others /icons/example2.jpg. When I ran it from Netbeans it work correctly, but the .jar did not work. After changing all the paths to /icons/ it worked perfectly.

    Thank you so much for this tip.