Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Having issues loading files using getClass().getResource()

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having issues loading files using getClass().getResource()

    I am using Intellij IDEA. For some reason the "Manage Attachments" button here doesn't do anything when clicked, so I've put an image showing the project tree up on imgur:
    https://imgur.com/a/jq8LeFO

    I am trying to access the file data.xlsx in /res/data/. It appears I have set up the resources folder correctly since it created the matching output folders and copied the files there.

    One of my files contains the line:
    dataFile = new XSSFWorkbook(getClass().getResource("/data/data.xlsx").toString());

    This results in an error:
    Caused by: java.io.FileNotFoundException: file:\C:\Users\t74bd\IdeaProjects\Phobos%20Project \out\production\Phobos%20Project\data\data.xlsx (The filename, directory name, or volume label syntax is incorrect)

    However when I specify the direct path:
    dataFile = new XSSFWorkbook("c:/Users/t74bd/IdeaProjects/Phobos Project/out/production/Phobos Project/data/data.xlsx");
    This works without error.

    I did notice that the error the first time included "file:\" before "C", so I tried this:
    dataFile = new XSSFWorkbook(getClass().getResource("/data/data.xlsx").toString().substring(6));
    This still gives the error:
    Caused by: java.io.FileNotFoundException: C:\Users\t74bd\IdeaProjects\Phobos%20Project\out\p roduction\Phobos%20Project\data\data.xlsx (The system cannot find the path specified)

    I also noticed that it has swapped backslashes with forward slashes, and is using an escape sequence %20. I'm assuming these aren't issues.

    EDIT: The last error message posted above appears to have a space in the word "production". That space only shows in the post, not when I edit the post (as I am now). It's not there, just some weird forum bug inserting a space.

  2. #2
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    88
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having issues loading files using getClass().getResource()

    java.io.FileNotFoundException: It could be that you cannot upload the file you want on the computer when the computer you are on is the computer to upload it on. The network shares files to be transferred over via the internet. So uploading a file on your computer to view it, is not possible. Sharing it over the internet and if it's your own file, this is possible.

Similar Threads

  1. Replies: 3
    Last Post: July 25th, 2012, 07:53 AM
  2. XMLBeans - how to control generated class files? windows vs linux issues
    By g20zoom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 6th, 2012, 12:01 PM
  3. add.(Recall.getResource());
    By DWH89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 10th, 2012, 08:45 AM
  4. getResource()-error
    By gargamel7 in forum Java Theory & Questions
    Replies: 6
    Last Post: September 3rd, 2011, 05:29 PM
  5. Replies: 15
    Last Post: September 2nd, 2011, 05:05 PM