0

Sorry if the question is basic. I’m trying to build a java project that include text files :

    File file = new File("data/include/foo.txt");

Problem is that with Netbeans, Built and Clean actions are done without taking into account external text files. Can you please help me solving this problem.

2
  • Do you want to include text files in the projects final jar? Commented Mar 27, 2014 at 21:12
  • Thank you for question. YES, without these files the application will not process! Commented Mar 28, 2014 at 3:36

1 Answer 1

1

In Netbeans there are basically two types of java projects. One is using ANT as its build machine and one is using maven.

Using ANT to build:

You have to modify or build a build.xml. Netbeans offers some targets in its standard build.xml to trigger events on each part of the build process.

So to include some additional resource files (in your case some text files) you should add your build.xml with a target like

<target name="-post-jar">
    <jar destfile="dist/myjar.jar" update="true">
        <fileset dir="${basedir}">
            <include name="data/include/*"/>
        </fileset>
    </jar>
</target>

It means that after your JAR is build the files from ${basedir}/files/* are included as well.

Using MAVEN to build:

Netbeans uses here completely Mavens infrastructure. So Maven does have a standard mechanism to recognize resource files to include. It all comes down to place these files in a specific place of the project. Assuming you did not change this standard directory layout of maven JAR projects is is something like:

Project
   src
       main
           java
           resources
   ...

So all files placed in src/main/resources (including subfolders) are included in your JAR automatically.

Read more here

Edit 1:

To access this resource files you cannot use File objects. This is done using the class own getResource methods:

App.class.getResourceAsStream("data.xml")

App.class.getResource("data.xml")

Here my class is App and my datafile is data.xml which is in the same directory as my class. This is relative adressed. But if you use a path with a heading / then your path is JAR file absolute. Read more about this here.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you VERY MUCH for your clear response. I updated Build.xml and as you mentioned a new .jar file including txt files was added but when I execute the main jar file I get an error response indicating that the path was not found (java.io.FileNotFoundException: data\include\file.txt). Is there anything else I should do?
Sorry, I'm not yet able to vote; vote requires 15 reputations!
You are right for vote up, which requires 15 points. But you should be able to accept answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.