0

So we got this assignment in a basic java programming course and we're supposed to implement a kind of card deck. To help us with this they have given us resources that will present a GUI on the screen, but when running my program I get a IOException that says that it can't read the input file, most likely since the pathname is wrong. And I dont know how to fix it, we're not even supposed to be in meddling with this code. The error is thrown in this method:

private Image getImg(Card aCard) {
    File pathToFile = null;
    if (aCard == null) {
        pathToFile = new File("cardset-oxymoron/shade.gif");
    } else {
        String suits = "cdhs";
        char c = suits.charAt(aCard.getSuit());
        String fileName = String.format("%s/%02d%c.gif", "cardset-oxymoron", aCard.getRank(), c);
        pathToFile = new File(fileName);
    }
    Image img = null;
    try {
        img = ImageIO.read(pathToFile);
    } catch (IOException ex) {
        System.err.println("Failed to create image");
        ex.printStackTrace();
    }
    return img;
}

And according to the error stack(?) it is at line 99, which is the

img = ImageIO.read(pathToFile); 

line

The folder that the cards are in is inside the project folder, right in between bin and src. using IntelliJ debugger I can see that the the pathToFile is "cardset-oxymoron\02d.gif". The filename is correct as all the cards are "[01-13][c/d/h/s].gif". When I rightclicked and copied the path to the files inside IntelliJ it was using forwardsslashes and not backslashes. But then I checked in explorer and it was the other way around... I have no idea where this is going wrong, any input would be greatly appreciated!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    So where is `cardset-oxymoron/shade.gif` relative to your class files, your jar, your program? Do you know what the user directory is? Have you tested to see where Java is in fact looking? Have you tried using resources and not files? Have you searched for similar questions as this sort of thing has been asked here *a lot*, and the answers are often the same. If this were my program, I'd put my images inside of the jar file and obtain them via resources and not files. – Hovercraft Full Of Eels Oct 23 '18 at 21:00
  • Java can use `/` path delimiter in Windows, Linux and MacOSX so it's not a problem. I prefer always use forward-slash in java application path values. Make sure a working dir is the application folder when you run the program. This `cardset-oxymoron/shade.gif` is a relative to a working(current) dir of the process. – Whome Oct 23 '18 at 21:00
  • @HovercraftFullOfEels Sorry if it's a repost of significan't magnitude, but I dont really get all the code myself so didn't know what to search for, especially with that String.format. But where would I put the jar folder and how would I go about accessing them as resources and not files? – PatchingMatching Oct 23 '18 at 21:09
  • Please check out these links: 1) [How do I load a file from resource folder](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder), 2) [File loading by getClass().getResource()](https://stackoverflow.com/questions/14089146/file-loading-by-getclass-getresource), 3) [Get a resource using getResource()](https://stackoverflow.com/questions/2593154/get-a-resource-using-getresource), and also [these search links](https://www.google.com/search?q=java+get+resource+file+site:stackoverflow.com) – Hovercraft Full Of Eels Oct 23 '18 at 21:14
  • Will do, thanks for the help! – PatchingMatching Oct 23 '18 at 21:18

1 Answers1

-1

According to your code your files are in directory cardset-oxymoron relative to your JVM run directory. I'm not sure about IntelliJ (I work all the time with Eclipse and Maven), but it could be bin directory.

You can check it by put those 2 lines to see what is it actually (somewhere before your actual code)

    File currentDir = new File("./");
    System.out.println(currentDir.getAbsolutePath());

Then your cardset-oxymoron must be in that directory. Or you can change file path appropriately.

E.g. if currentDir is bin then pathToFile will be

 pathToFile = new File("../cardset-oxymoron/shade.gif");

as well as fileName for other case.

Vadim
  • 4,027
  • 2
  • 10
  • 26