Git Product home page Git Product logo

Comments (8)

BrianNichols avatar BrianNichols commented on September 26, 2024

Can you provide the code snippet where register was called?
I would like to see how the classloader was obtained and the resourcePath used.
Also, where is the resource file located on disk?

from aerospike-client-java.

BrianNichols avatar BrianNichols commented on September 26, 2024

This will work if you place the file in "src/main/resources/udf/list_functions.lua" and you use maven to build "myjar.jar" and this jar is placed in the war file "WEB-INF/lib/myjar.jar".

ClassLoader cl = Thread.currentThread().getContextClassLoader();
client.register(policy, cl, "udf/list_functions.lua", "list_functions.lua", Language.LUA);

from aerospike-client-java.

kevinmic avatar kevinmic commented on September 26, 2024

Here is my exact code, it is a list of file names, all of these files are under the udf directory. It is nearly identical to your code.

for (String udfResource : UDF_RESOURCES) {
            client.register(client.readPolicyDefault, Thread.currentThread().getContextClassLoader(), "udf" + File.separator + udfResource, udfResource, Language.LUA);
}

Also, I am building my project with maven. I have multiple builds, one builds the jar, the other the war. The file structure in my war is a typical war structure

META-INF/
WEB-INF/lib/myjar.jar

My jar contains the following file structure

udf/list_functions.lua

The problem is in your register function you do this --

File file = new File(resourceLoader.getResource(resourcePath).getFile());

java.io.File is not designed to read things out of an .jar archive. I have created a github project that illustrates the issue.

https://github.com/kevinmic/java_readFileFromResource

from aerospike-client-java.

BrianNichols avatar BrianNichols commented on September 26, 2024

We tried your test repo and it originally failed like you described.

Then we changed your last run instruction and it worked fine.

Old:
java -classpath myjar.jar:. TestIt

New:
java -cp .:myjar.jar TestIt

java.io.File is designed to read things out of an .jar archive.
See: http://www.mkyong.com/java/java-read-a-file-from-resources-folder

from aerospike-client-java.

kevinmic avatar kevinmic commented on September 26, 2024

I have fixed my example. Changing the classpath order was allowing the classloader to find the udf directory in the filesystem (because . was loaded first). I changed the example so that the udf directory is not in the same directory as the runtime of the program. It now errors no matter how you specify the classpath.

The mkyong example is showing how to read a file from the resource directory and not an example of how to read a resource from a jar file. He is using maven to compile and test a given class but maven does not generate a jar file in the compile, test-compile or test-phase. If you run maven in debug mode (mvn -X test) you will see the test classpath is similar to this.

[DEBUG] test classpath: /apps/code/git/ax/model/target/test-classes /apps/code/git/ax/model/target/classes

So the target/test directory is included in the classpath and then the target/classes directory is included. The target/classes directory will contain all resources and all compiled classes, which is why the mykong example works.

Looking at the documentation here - https://docs.oracle.com/javase/6/docs/api/java/io/File.html - there doesn't seem to be anything suggesting that java.io.File will handle anything but regular filesystem operations.

from aerospike-client-java.

BrianNichols avatar BrianNichols commented on September 26, 2024

Ok, I see your point. Our tests worked because the udf directory existed on the filesystem as well as the jar. Our war test also worked because tomcat expands the war file, so "WEB-INF/classes/udf" existed on the filesystem too.

We will fix in the next java client release.

from aerospike-client-java.

luando avatar luando commented on September 26, 2024

Hi,
I want to know in Aerospike Client Java 3.2, this error is fixed?

Thanks

from aerospike-client-java.

BrianNichols avatar BrianNichols commented on September 26, 2024

Yes, that was fixed 9 months ago.

from aerospike-client-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.