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 4 of 4

Thread: how to include and access a data file in a jar

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to include and access a data file in a jar

    I would like to add some data files to my class jar and access them.
    package urltest;
     
    import java.net.URL;
    public class UrlTest {
    public String getImage(String dirName) {
            URL folder = UrlTest.class.getResource( dirName );
            if( folder != null ) {
                return ( folder.getPath() );
            }
            System.out.println(dirName + "  not found!!!");
            return null; //example.txt TODO: Better error handling
        }
     
        public static void main(String[] args) {
        String dirName=".";
        String dirPath;
     UrlTest myFolder = new UrlTest(); 
     dirPath=myFolder.getImage(dirName);
        System.out.println("Directory path=" + dirPath);
     
    }
    }

    when I build and excute this progam I get
    java -jar UrlTest.jar
    . not found!!!
    Directory path=null

    If update the jar file with the files I am unable to find them .
    Greatly appreciate help
    Peter
    Last edited by Norm; August 6th, 2019 at 08:39 AM. Reason: Added []s to code tags. Fixed /s

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to include and access a data file in a jar

    One way to read files from a jar file with code from within the jar file is to use one of the Class class's getResouce methods.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to include and access a data file in a jar

    Quote Originally Posted by Norm View Post
    One way to read files from a jar file with code from within the jar file is to use one of the Class class's getResouce methods.
    Hi Norm
    I do not understand your answer.
    I added getPathName to the class ran it in netbeans then
    ran clean and build and executed the jar file.
    The results show that get resources mehtod returns null
    but the straight pathname points to the same directory as the jar file
    Really would like to find the answer. Thanks
    ************************************************** ****

    package urltest;
     
    import java.io.File;
    import java.net.URL;
    public class UrlTest {
    public String getUrl(String dirName) {
            URL folder = UrlTest.class.getResource( dirName );
            if( folder != null ) {
                return ( folder.getPath() );
            }
            System.out.println(dirName + "  URL  not found!!!");
            return null; //example.txt TODO: Better error handling
    }
     public String getPathName( String pathName)  {
         File file = new File(pathName); 
         if (file != null){
             return (file.getAbsolutePath());
         }
             System.out.println("getPathName Not Found "+ pathName);
             return (null);
         }
     
     
        public static void main(String[] args) {
        //String dirName="example.txt";
            String dirName=".";
        String dirPath;
     UrlTest myFolder = new UrlTest(); 
     dirPath=myFolder.getUrl(dirName);
        System.out.println("URL path=" + dirPath);
     dirPath = myFolder.getPathName(dirName);
            System.out.println("Directory Path= " + dirPath);
    }
    }
    run:
    URL path=/home/pdk/NetBeansProjects/UrlTest/build/classes/urltest/
    Directory Path= /home/pdk/NetBeansProjects/UrlTest/.
    BUILD SUCCESSFUL (total time: 0 seconds)

    Performed Clean and Build.

    pdk@pdk64:~/NetBeansProjects/UrlTest/dist$ java -jar UrlTest.jar
    . URL not found!!!
    URL path=null
    Directory Path= /home/pdk/NetBeansProjects/UrlTest/dist/.
    ************************************************** ******************
    code/


    --- Update ---

    Quote Originally Posted by Norm View Post
    One way to read files from a jar file with code from within the jar file is to use one of the Class class's getResouce methods.
    Hi Norm
    I do not understand your answer.
    I added getPathName to the class ran it in netbeans then
    ran clean and build and executed the jar file.
    The results show that get resources mehtod returns null
    but the straight pathname points to the same directory as the jar file
    Really would like to find the answer. Thanks
    ************************************************** ****

    code/
    package urltest;

    import java.io.File;
    import java.net.URL;
    public class UrlTest {
    public String getUrl(String dirName) {
    URL folder = UrlTest.class.getResource( dirName );
    if( folder != null ) {
    return ( folder.getPath() );
    }
    System.out.println(dirName + " URL not found!!!");
    return null; //example.txt TODO: Better error handling
    }
    public String getPathName( String pathName) {
    File file = new File(pathName);
    if (file != null){
    return (file.getAbsolutePath());
    }
    System.out.println("getPathName Not Found "+ pathName);
    return (null);
    }


    public static void main(String[] args) {
    //String dirName="example.txt";
    String dirName=".";
    String dirPath;
    UrlTest myFolder = new UrlTest();
    dirPath=myFolder.getUrl(dirName);
    System.out.println("URL path=" + dirPath);
    dirPath = myFolder.getPathName(dirName);
    System.out.println("Directory Path= " + dirPath);
    }
    }

    run:
    URL path=/home/pdk/NetBeansProjects/UrlTest/build/classes/urltest/
    Directory Path= /home/pdk/NetBeansProjects/UrlTest/.
    BUILD SUCCESSFUL (total time: 0 seconds)

    Performed Clean and Build.

    pdk@pdk64:~/NetBeansProjects/UrlTest/dist$ java -jar UrlTest.jar
    . URL not found!!!
    URL path=null
    Directory Path= /home/pdk/NetBeansProjects/UrlTest/dist/.
    ************************************************** ******************
    code/

    --- Update ---

    Jst read the second thread you sent me , I'll stick with keeping the data files outside the jar.
    Thanks again. Peter
    Last edited by Norm; August 7th, 2019 at 05:30 AM. Reason: fixed code tags

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to include and access a data file in a jar

    get resources mehtod returns null
    That would mean that the path it was given was wrong.
    Here is the output I get when I execute the code from a class file not in a package and not in a jar:
    URL path=/D:/JavaDevelopment/Testing/ForumQuestions12/UrlTest.java
    Directory Path= D:\JavaDevelopment\Testing\ForumQuestions12\UrlTes t.java
    From inside of a jar file:
    D:\JavaDevelopment\Testing\ForumQuestions12\Testin g\TestPackages>java -jar _Test2.jar
    URL path=file:/D:/JavaDevelopment/Testing/ForumQuestions12/Testing/TestPackages/_Test2.jar!/Test2/UrlTest.java
    Directory Path= D:\JavaDevelopment\Testing\ForumQuestions12\Testin g\TestPackages\UrlTest.java

    D:\JavaDevelopment\Testing\ForumQuestions12\Testin g\TestPackages>MORE
    Try fixing the path and filename


    Here is how to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  2. How to include .ini file into .jar file
    By deemu in forum Java Theory & Questions
    Replies: 5
    Last Post: March 6th, 2012, 10:00 AM
  3. How to include .ser file into .jar file
    By yuki in forum Java Theory & Questions
    Replies: 17
    Last Post: April 19th, 2010, 10:43 PM

Tags for this Thread