0

Platform: Java SWT using Eclipse IDE

Before continuing, I did see a similar issue here on Stack Overflow, 19035407.

Source:

package prjMyapp;

import java.io.InputStream;

public class Myapp
{
    public static void main(String[] args)
    {
    InputStream oStream = Myapp.class.getResourceAsStream("resources/StarTrekLandruRocks.txt");
    if (null == oStream)
        System.out.println("input stream is null");
    else
        System.out.println("input stream is NOT null :-)");
    }
}

Document Placement

prjMyApp / src / prjMyApp
--> All .java files
--> resources
    --> Non-image resources
--> images
    --> Image resources (jpg, png)

Interesting Note

I used to use /prjMyapp/resources/whatever.txt and /prjMyapp/images/whatever.ext, however during testing to figure out this problem, I realized that a relative path works too and is better visually and otherwise, hence "resources/StarTrekLandruRocks.txt" instead of the previous "/prjMyapp/resources/StarTrekLandruRocks.txt". As stated, images, and I have quite a few, work as expected on all machines (even a Mac), just not the text file.

Development Machine:

My development machine executes the code perfectly no matter what I do.

CENTOS Box

My CENTOS (Linux) box executes the self contained all inclusive jar file, for which I use ANT to create the file. I can load the text file, if I have the text file as a png extension, but I cannot load the file as a txt extension, same everything else.

Yes, I know that renaming a txt file to a png extension ought never to be done, but I was curious what would happen, as I can load images (jpg and png) with no issues. I found out that getSystemClassLoader just does not like a txt extension. Renaming the extension as png is a hack and fakes the method into working for whatever reason.

I prefer to not do a hack.

Why does the getSystemClassLoader not work directly with txt extension? According to the other post it should, but not on my system.

What is the fix?

Community
  • 1
  • 1
Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130
  • 1
    where is your text file placed inside jar / – jmj Jun 23 '14 at 20:11
  • See modified question above. – Sarah Weinberger Jun 23 '14 at 20:22
  • Your question is therefore not clear. Please clarify using different wording. If your question is about the binary format of the jar file, then that is a question for Oracle or whomever wrote ANT and Eclipse. In Eclipse, I created the folder structure as indicated. If you are asking for the XML fragment in my build.xml script, then let me know and I can copy and paste that above, otherwise please clarify. – Sarah Weinberger Jun 23 '14 at 20:42
  • 1
    Jigar is asking about the contents in your jar. The jar file is just a zip file, and you can unzip its contents to check if the text file is present there or not. – Luiggi Mendoza Jun 23 '14 at 20:43
  • Could you please post the directory structure of jar file (that includes your text file along with its parent directories, ignoring other files) – jmj Jun 23 '14 at 20:43
  • Hmm, that is interesting. I built a jar with the file as a png, and the jar file contained the path "prjMyapp/resources/StarTrekLandruRocks.png" as expected. I then renamed the text file back to a .txt extension and rebuilt the jar. After extracting the contents, I was surprised to not only not see the text file but I did not see the resources folder. The issue is obviously a compile issue and not a loading issue. – Sarah Weinberger Jun 23 '14 at 21:53
  • That led me to the answer. I am still learning Ant and did not see the resources section at the top. – Sarah Weinberger Jun 23 '14 at 21:55

1 Answers1

1

The comments led me to the answer.

The method, getResourceAsStream(), worked. I just did not realize that the file was simply not in the jar as a txt extension, therefore the failure.

    <target name="jarResources" depends="jarCode">
        <jar destfile="${jar.code}" update="true">
            <fileset dir="${src.dir}" includes="**/*.png" />
            <fileset dir="${src.dir}" includes="**/*.jpg" />
            <fileset dir="${src.dir}" includes="**/*.ico" />
            <fileset dir="${src.dir}" includes="**/*.txt" />
        </jar>
    </target>

I was missing the last line in the jarResources section. It just had png, jpg, and ico originally. Adding in the txt line resolved the issue.

Issues can be in the build script, not just in Java, lesson learnt. Thanks.

Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130