1

I'm currently trying to develop a game, and the file path is changing when I export it.

Here is the code:

package random;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Troll extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 4176461585360667597L;
    public static BufferedImage img;
    public static void main(String[] args){
        File f = new File("troll.png");
        try{
            if(f.exists()){
                System.out.println("ITS THERE! :D");
                   img = ImageIO.read(f);
            } else {
                System.out.println("DOESNT EXIST, REAL PATH IS: " + f.getAbsolutePath() );
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        new Troll();

    }
    public Troll(){
        init();
    }
    public void init(){
        setSize(1200,800);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    public void paint(Graphics g){
        g.drawImage(img, 500, 350, this);
    }
}

When I run it through Eclipse ( The IDE I'm using ), it's running fine and it's showing the image. When I export it as a jar and convert it to an exe using Jar2Exe Software, the image does not appear and in the console it says that the absolute path for the image is on my Desktop. But when I open the exe using 7-Zip, the picture is in the exe.

How can I export it so that when the user runs it, the program can find the file path and show the image, instead of it thinking that it's on my desktop?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
joey942
  • 159
  • 2
  • 11
  • Alluded to by both answers, but further: Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jul 23 '16 at 04:15

2 Answers2

2

If you want to publish it as a jar then you need to use the Jar-specific API for reading your file. See eg. How to read a file from a jar file? (and you need to configure Eclipse to put the picture in the jar, which it sounds like you're already doing).

Also you should let us know whether it works when it's in a jar but not as an exe, that will help us narrow down where the problem may be.

Community
  • 1
  • 1
TubesHerder
  • 181
  • 6
  • A bit new to this whole part of java, files and such, so could you maybe give me an example? The answer to that post you linked me was to use getResourceAsStream(), which didn't work. Unless I'm approaching this wrong... – joey942 Jul 22 '16 at 22:48
  • I may just be having a stupid moment, but what do you mean let you know if it works when it's in a jar? Do you mean run it as a jar...? – joey942 Jul 22 '16 at 22:51
  • yes I mean run it as a jar. Like this: java -jar myfile.jar – TubesHerder Jul 22 '16 at 23:05
  • The example would be exactly what chikincrow is suggesting below. – TubesHerder Jul 22 '16 at 23:06
1

I hope this is not a troll (lol)

img = ImageIO.read(this.getClass().getResource("/troll.jpg"));

You are in a jar, there is no resource file.


See that link as well: http://www.jar2exe.com/createdexe/integrate/protect

Thread.currentThread().getContextClassLoader().getResource("hello/yes.gif");
chikincrow
  • 393
  • 3
  • 11
  • I tried using that instead, still would not show as an exe. – joey942 Jul 22 '16 at 22:50
  • Try that maybe (http://www.jar2exe.com/createdexe/integrate/protect) Thread.currentThread().getContextClassLoader().getResource("hello/yes.gif"); – chikincrow Jul 22 '16 at 22:59