Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1.0 AerospikeClient.register in war does not work #26

Closed
kevinmic opened this issue Mar 31, 2015 · 8 comments
Closed

3.1.0 AerospikeClient.register in war does not work #26

kevinmic opened this issue Mar 31, 2015 · 8 comments

Comments

@kevinmic
Copy link

I tried upgrading to the latest aerospike client version and had an error trying to use the new register function from within a war.

AerospikeClient.register(readPolicy, contextLoader, resourcePath, name, Language.LUA)

Caused by: java.io.FileNotFoundException: /content/mywar.war/WEB-INF/lib/myjar.jar/udf/list_functions.lua (No such file or directory)
at java.io.FileInputStream.open(Native Method) [rt.jar:1.7.0_71]

@BrianNichols
Copy link
Member

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?

@BrianNichols
Copy link
Member

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);

@kevinmic
Copy link
Author

kevinmic commented Apr 1, 2015

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

@BrianNichols
Copy link
Member

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

@kevinmic
Copy link
Author

kevinmic commented Apr 2, 2015

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.

@BrianNichols
Copy link
Member

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.

@luando
Copy link

luando commented Mar 2, 2016

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

Thanks

@BrianNichols
Copy link
Member

Yes, that was fixed 9 months ago.

ijusti pushed a commit to ijusti/aerospike-client-java that referenced this issue Mar 28, 2023
ijusti pushed a commit to ijusti/aerospike-client-java that referenced this issue Mar 28, 2023
…ike_template_misc_methods

ReactiveAerospikeTemplate implementation (miscellaneous methods)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants