d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Problem With A Jarfile > Works On My Not Others
Add Reply New Topic New Poll
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Jun 5 2013 02:47pm
Have made a small game that I want to share with others.

Netbeans lets me generate a jar file.

It works perfect on my own computer. But on others a part of it doesnt work.

Am pretty sure its only the functionality in this method which causes problems.

Maybe its the declaration of a file in line 3.

Can someone help ?

Code
   private void verdenskortMouseClicked(java.awt.event.MouseEvent evt) {                                        
       hint.setText(givhint());
      File file = new File("src/Kort/verden.jpg");

       try {            
           BufferedImage image = ImageIO.read(file);
           int x = verdenskort.getMousePosition().x;
           int y = verdenskort.getMousePosition().y;            
           int farve=image.getRGB(x,y);
           //System.out.println("x "+x+" y "+y);
           if("Antarktis".equals(givOmr(farve,x,y))){
           detsker.setText("Antarktis");
           }
           if("Sydlige Ishav".equals(givOmr(farve,x,y))){
           detsker.setText("Sydlige Ishav");
           }
           if("Arktiske Ocean".equals(givOmr(farve,x,y))){
           detsker.setText("Arktiske Ocean");
           }
           if("Atlanterhavet".equals(givOmr(farve,x,y))){
           detsker.setText("Atlanterhavet");
           }
           if("Indiske Ocean".equals(givOmr(farve,x,y))){
           detsker.setText("Indiske Ocean");
           }
           if("Stillehavet".equals(givOmr(farve,x,y))){
           detsker.setText("Stillehavet");
           }
           if("Nordamerika".equals(givOmr(farve,x,y))){
           detsker.setText("Nordamerika");
           }
           if("Sydamerika".equals(givOmr(farve,x,y))){
           detsker.setText("Sydamerika");
           }
           if("Europa".equals(givOmr(farve,x,y))){
           detsker.setText("Europa");
           }
           if("Australien".equals(givOmr(farve,x,y))){
           detsker.setText("Australien");
           }
           if("Asien".equals(givOmr(farve,x,y))){
           if(findkort()!=-2){
               if(s.spiller1tur==true){
                   System.out.println("kort "+findkort());
               s.asien[s.hand1[findkort()]][0]=s.asien[s.hand1[findkort()]][0]+100;
               s.hand1[findkort()]=s.bunke1[s.runde+5];
               s.tidgaa();
               tegndominans();
                tegnforan();
               }
                               if(s.spiller1tur==false){
               s.asien[s.hand2[findkort()]][1]=s.asien[s.hand2[findkort()]][1]+100;
               s.hand2[findkort()]=s.bunke2[s.runde+5];
               s.tidgaa();
               tegndominans();
                tegnforan();
               }
               skifttur();
               slutkontrol();
           }
       detsker.setText(kontinentstatus("asien"));
       afrilabel.setBorder(null);
       asilabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
           }
           if("Afrika".equals(givOmr(farve,x,y))){
                       if(findkort()!=-2){
               if(s.spiller1tur==true){
                   System.out.println("kort "+findkort());
               s.afrika[s.hand1[findkort()]][0]=s.afrika[s.hand1[findkort()]][0]+100;
               s.hand1[findkort()]=s.bunke1[s.runde+5];
               s.tidgaa();
               tegndominans();
                tegnforan();
               
               }
                               if(s.spiller1tur==false){
               s.afrika[s.hand2[findkort()]][1]=s.afrika[s.hand2[findkort()]][1]+100;
               s.hand2[findkort()]=s.bunke2[s.runde+5];
               s.tidgaa();
               tegndominans();  
                tegnforan();
               }
           skifttur();
           slutkontrol();
           }
                     detsker.setText(kontinentstatus("Afrika"));
       afrilabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
       asilabel.setBorder(null);          
           }
/*String foo = "Spiller 1 Afrika: Harer "+String.valueOf(s.afrika[0][0])+" Elefanter "+String.valueOf(s.afrika[1][0])+" Geparder "+String.valueOf(s.afrika[2][0]+"     ");  
String bar = foo.concat("Spiller 2 Afrika: Harer "+String.valueOf(s.afrika[0][1])+" Elefanter "+String.valueOf(s.afrika[1][1])+" Geparder "+String.valueOf(s.afrika[2][1])+"--------");
String baz = bar.concat("Spiller 1 Asien: Harer "+String.valueOf(s.asien[0][0])+" Elefanter "+String.valueOf(s.asien[1][0])+" Geparder "+String.valueOf(s.asien[2][0]+"     "));
String quz = baz.concat("Spiller 2 Asien: Harer "+String.valueOf(s.asien[0][1])+" Elefanter "+String.valueOf(s.asien[1][1])+" Geparder "+String.valueOf(s.asien[2][1]));
detsker.setText(quz);*/
           
       } catch (IOException ex) {
           Logger.getLogger(Spilleplade.class.getName()).log(Level.SEVERE, null, ex);
       }
       tegndominans();
       tegnforan();
   }    
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 5 2013 06:54pm
http://stackoverflow.com/questions/2393194/how-to-access-resources-in-jar-file

1) why do you have a src folder in your jar?
2) you seem to know how to use a logger. why aren't you logging to determine the problem?
3) if (!file.exists()) // handle error

This post was edited by carteblanche on Jun 5 2013 06:55pm
Member
Posts: 4,980
Joined: Jan 16 2010
Gold: 0.00
Warn: 20%
Jun 5 2013 07:51pm
And code in English. It's pretty vital for code you wish to share that it is English. Folk aner sgu ikke hvad harer og geparder er. ;)
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Jun 6 2013 06:37am
Thanks for advise both of you.

Dont know much about jar files. Java just generates it for me.

The jar file works perfect on my own computer but not perfect on others.

all the pictures show up as they should. and you are able to click most of them as you should.

But there is a world map where you should be able to click. On other computers than mine nothing happens when you do.

So pretty sure it is this method that is causing the problem in the jar file.
Quote (carteblanche @ Jun 6 2013 01:54am)
http://stackoverflow.com/questions/2393194/how-to-access-resources-in-jar-file

1) why do you have a src folder in your jar?
2) you seem to know how to use a logger. why aren't you logging to determine the problem?
3) if (!file.exists()) // handle error


1. Dont understand. I can not open my jar file. When I click on it the program runs. My netbeans project is called Gratisdyr. in "src" folder there are "Kort" folder with pictures and "gratisdyr" folder with logical classes.
2. I dont know how to use a logger. How do u do that ?
3. The file does exist when I run netbeans. But not sure whether it does or not when others runs my jar file.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 6 2013 02:38pm
jar files are just zip files with your class files and resources inside. you'll see a manifest which provides the classpath and tells you which main method is invoked.

rename xxx.jar to xxx.zip and unzip it and you can see whats inside.

Quote
2. I dont know how to use a logger. How do u do that ?

it's this.... though it's best to create the logger object as a static field in the class
Code
} catch (IOException ex) {
          Logger.getLogger(Spilleplade.class.getName()).log(Level.SEVERE, null, ex);
      }

Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Jun 7 2013 12:49pm
Thanks.

Think I understand it a bit better now.

So the code line:
File file = new File("src/Kort/verden.jpg");
will make no sense on a computer different than mine ?

But a line like this setting a label is working fine.
k1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Kort/"+s.hand1[0]+".jpg"))); //
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Jun 7 2013 02:53pm
This works..

InputStream is = Main.class.getResourceAsStream("verden.jpg");
BufferedImage image = ImageIO.read(is);

Finally ot in.
Thanks all
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll