简体   繁体   中英

Jar file won't display resource after update from 7 to 10

Often I use a certain jar file which displays a text file and allows you to filter the list in different ways.

This worked perfectly until I updated from Java 7 to Java 10. Now it won't display the resource text file anymore.

Here is the where I got the jar file: https://github.com/dragan224/battle_cats_en_combos

In the root of the jar file are two .txt files which the jar uses inside of itself. Is there some security setting that blocks this? Or did the version change make the code defunct?

In the code files, I can see it is using this: InputStream input = (InputStream) ClassLoader.class.getResourceAsStream("/" + cat_file_name);

So, I've recompiled it and specified -source 1.8 in the compilation string in the commandline, but it doesn't make any difference.

I don't even know how to figure out why it's not working. When I run the commandline javaw -jar CatCombos.jar (my new compilation's name), it doesn't say anything on the commandline in response. It runs, but doesn't display the text file, just like the original one.

This all started when I installed jdk-10.0.2_windows-x64_bin.exe just today. Before that I was running 1.8 JRE and it worked fine.

[update]

I installed JDK 8.44 and ran the jar files with the javaw.exe from that folder and the jar application runs perfectly and displays the contents just fine. So something is changed or I'm missing something to make Java 10 run the files properly.

[update]

Okay, so my question about using Java was migrated to Stack Exchange even though it was not a question about coding. This was a mistake by somebody, and since this is now believed to be a coding question (in which no code was supplied in the first place) I'm going to suppose that the reason Java 10 didn't display the text file is because the code used for Java 8 is no longer viable. Therefore, I'm going to present code and maybe resolve the question this way by turning it into a coding question.

Here is the code where I am loading the text file.

        InputStream input = (InputStream) ClassLoader.class.getResourceAsStream("/" + file_name);
        BufferedReader br = new BufferedReader(new InputStreamReader(input));
        String line = br.readLine();
        while (line != null) {
        //do something
        }

So, for the structure of the project, it is very simple, and goes like this (this is the first java project I've ever worked with, by the way):

/src
/src/javafiles (no subfolders; just .java files)
/src/textfile.txt  (the one I'm trying to load)
/manifest.txt (telling which java file to start up with)
/compile.bat

So, the textfile is in the parent folder of the java binaries.

[update]

After loading up the project into Eclipse (which I've never used before), I am seeing that I am getting a null pointer error on this line:

InputStream input = (InputStream)ClassLoader.class.getResourceAsStream(combo_file_name);

Eclipse gives this description:

Open Declaration InputStream java.lang.Class.getResourceAsStream(String name)

@CallerSensitive

Finds a resource with a given name.

If this class is in a named Module then this method will attempt to find the resource in the module. This is done by delegating to the module's class loader findResource(String,String) method, invoking it with the module name and the absolute name of the resource. Resources in named modules are subject to the rules for encapsulation specified in the Module getResourceAsStream method and so this method returns null when the resource is a non-".class" resource in a package that is not open to the caller's module.

Otherwise, if this class is not in a named module then the rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm: • If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'. • Otherwise, the absolute name is of the following form: modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\.').

Parameters:name name of the desired resourceReturns:A java.io.InputStream object; null if no resource with this name is found, the resource is in a package that is not open to at least the caller module, or access to the resource is denied by the security manager.Throws:NullPointerException - If name is nullSince:1.1See Also:Module.getResourceAsStream(String)@revised9@specJPMS

So, what do I do with this? I'm thinking the .jar can't find the text file for some reason.

Here is how I address the files:

package catcombo;

public class Main {

    public static void main(String[] args) {
        new MainWindow("NyancomboData.txt", "Names.txt");
    }

}

In the updates from 1.8 to version 10, there must have been introduced a new strictness with the use of static methods within a class.

My code had

InputStream input = (InputStream) ClassLoader.class.getResourceAsStream("/" + file_name);

When I changed it to

InputStream input = MyOwnClass.class.getResourceAsStream(file_name);

Then everything began to work perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM