Playframework messages loading issue - java

I have a play framework (2.2.3) project which also uses maven. One of the dependencies in my project is org.apache.wss4j.wss4j-ws-security-common-2.1.4.jar. This JAR has a messages folder in classpath. On starting play I get the following error (I am just pasting a few lines here)
Oops, cannot start the server.
MainException: class java.lang.NullPointerException(null)
class java.lang.NullPointerException(null)
java.io.FilterInputStream.read(FilterInputStream.java:133)
play.api.i18n.Messages$MessagesParser.parse(Messages.scala:216)
If I remove the messages folder from this JAR, play starts normally. I assume that Play tries to load all messages in the classpath and expects them to be files - it breaks because the messages in the WSS4J jar is a folder.
How can I resolve this error?

The way I solved this problem was changing the name to the folder that contains the "wss4j_errors.properties" file.
In code, edit line 42 at src/main/java/org/apache/wss4j/common/crypto/WSS4JResourceBundle.java
Run the tests, compile and try the new jar.
Not the answer I would like, but it works.

Related

How do I use a classloader to get inputstream on a model.zip file where the zip file is wrapped in a .jar file in classpath

The issue:
We have a jetty web-app, and in the application-code, I am trying to access a zip within a jar in classpath. Here's the jar in the libs folder:
/path/to/app/x.x.0-SNAPSHOT/apps/libs/my-model.jar
where my-model.jar is really just an empty folder with model.zip file inside it. If I extract this jar, I get johnsnow/mymodel.zip
My application code tries to access this zip as:
getClass().getResourceAsStream("johnsnow/mymodel.zip")
but of course, I don't get a proper handle to this resource and wind up getting a nullpointer exception. What am I doing wrong? Shouldn't I be able to access a file within a jar file in classpath using the getClass().getResourceAsStream() method?
Footnote:
Because model.zip was too large, we decided against shipping it with the code base. Thus we pushed it into a nexus repository, and reference the jar via a gradle compile dependency as follows:
compile "com.company.group.nlp:my-model:1.0#jar"
The fact that building the distribution pulls this jar, and puts it in apps/libs tells me that gradle does its part (of downloading the dependency to a classpath). The issue remains that I can't seem to find a way to access mymodel.zip inside my-model.jar
Try adding a slash to the file path:
getClass().getResourceAsStream("/johnsnow/mymodel.zip");
It will tell java to start looking for the class from the root folder, not from the current class package.

Unable to locate a file in java project running in a executable jar

Both class.getResource(FILE_NAME) and class.getClass().getClassLoader().getResource(FILE_NAME) run perfectly inside my eclipse but the same code getting failed to locate the file which is inside the jar file, when run as an executable jar in windows machine.
I have gone through all related links available for this problem (well, not exactly the same issue but 90% in sync), asked for solution but no reply came from any of those posts, so I'm posting my issue as a separate question hoping for help on this.
In total, 4 cases I have ran to resolve but none worked so far and I'm out of ideas now.
class.getClass().getClassLoader().getResource("/resources/readme.txt");
class.getResource("/resources/readme.txt");
class.getClass().getClassLoader().getResource("resources/readme.txt");
class.getResource("resources/readme.txt");
Ouf of all the above 4 cases, only 2 cases ran successfully in eclipse which are as mentioned below.
class.getResource("/resources/readme.txt");
class.getClass().getClassLoader().getResource("resources/readme.txt");
The other 2 cases just throwing me Exception in thread "main" java.lang.NullPointerException
Coming to the executable jar, all 4 cases are throwing me the Exception in thread "main" java.lang.NullPointerException.
So I have created a folder named resources where my jar is residing and placed my files inside this folder and ran the jar. Now the jar is running without any issues referring to the files inside the resources folder I created. So wherever I run this jar (windows, linux etc.,) I need to create a resources folder and place my files under the folder. Now the question is, can it be possible to make my jar refer the resources folder which is inside the jar itself?
Any help on this is much appreciated!
To get your txt file:
File yourFileIsHere = new File("resources/readme.txt");
Where put your file?
In the same location of your jar, example:
myapp/yourjar.jar
myapp/resources/readme.txt
If you want read file inside of your "src" folder:
InputStream yourInputStream = new YourClass().getClass().getClassLoader().getResourceAsStream("readme.txt");
If you are using Spring:
org.springframework.util.ResourceUtils.getFile("classpath:readme.txt")
Otherwise:
import com.google.common.io.Resources
byte[] byteSource = Resources.asByteSource(Resources.getResource("readme.txt")).read()
method class.getClass().getClassLoader().getResource() may take 3 prefixes: url:, classpath: and file: each prefix tells what is your base of search. If you want to search inside your jar use classpath: prefix. That tells your classloader to search everywhere within your classpath. Here is one example how to deal with it with Spring tools. Look also at ResourceLoader class in Spring

Problems with building neo4j GetAll Server-Plugin

I'm trying to build the neo4j GetAll plugin. Project contains only one example class from here.
When I try to run curl http://localhost:7474/db/data/ext/GetAll/graphdb/get_all_nodes, I get the error message:
No such ServerPlugin exception
I'm using neo4j-community-2.2.1 (on Windows) and jdk1.7.0_79.
My project consists of :
GetAll.java in org.neo4j.examples.server.plugins package
META-INF/services/org.neo4j.server.plugins.ServerPlugin file with this content: org.neo4j.examples.server.plugins.GetAll
in buildpath are all jars from %NEO4J_HOME%\lib included.
The jar-file is built using jar -cvf myext.jar * and than that jar placed in %NEO4J_HOME%\plugins.
I have no idea, why I cannot make it work properly. Any suggestions?
The screenshot clearly shows that GetAll.class is located in directory bin/org/neo4j/examples/server/plugins instead of org/neo4j/examples/server/plugins (get rid of the bin).

NTEventLogAppender is not working for my Java Application

I'm trying to write to the Windows Event Log and I have a nagging issue.
First I created the NTEventAppender using steps found here.
How to create NTEventlogAppender.dll as required for logging event in the eventlog using log4cplus
I placed the .dll in the System32 folder. When I built and ran the program in eclipse I received this error
java.lang.UnsatisfiedLinkError: org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Ljava/lang/String;Ljava/lang/String;)I
at org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Native Method)
at org.apache.log4j.nt.NTEventLogAppender.<init>(NTEventLogAppender.java:79)
at org.apache.log4j.nt.NTEventLogAppender.<init>(NTEventLogAppender.java:49)
at mahle.eolx.ws.Main.<clinit>(Main.java:30)
Exception in thread "main"
Do I need to do something differently since this is a Java application and the question linked above was for a C++ Application.
And if that is the case, what do I need to do.
EDIT: I found out that I should be using the NTEventLogAppender.dll that was included with the log4j download, but I'm still getting the same error.
I couldn't get this to work, however I was able to log to the event viewer by using log4jna for the NTEventLogAppender (https://github.com/dblock/log4jna).
I took these steps
Download log4j and include the .jar file in your project http://logging.apache.org/log4j/1.2/download.html
Download log4jna which is used to replace the NTEventLogAppender and add the necessary jars to your project (log4jna, jna, platform) http://log4jna.codeplex.com/
You will need to add the Win32EventLogAppender.dll somewhere in your application directory.
To get the logger to log meaningful messages you will need to change the registry
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Application\"YourAppNameHere"]
"EventMessageFile"="C:\\Program Files\\"Your Application"\\Win32EventLogAppender.dll"
"CategoryMessageFile"="C:\\Program Files\\"Your Application"\\Win32EventLogAppender.dll"
"TypesSupported"=dword:00000007
"CategoryCount"=dword:00000006

How to find hidden jar in classpath?

I have a web app with Spring, Hibernate and Struts 2 and I get this error:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoSuchFieldError: TRACE
So I googled it, found this wich says:
if you are on the Equinox Console, perform the following check:
packages org.apache.log4j
Which I did, and got this:
org.apache.log4j; version="1.2.15"<org.apache.log4j_1.2.15.v201005080500 [33]>
org.apache.velocity_1.5.0.v200905192330 [37] imports
And I don't really know what that means... But what I'm sure of is that jar is NOT the one I'm supposed to be using.
In fact, I ran the packages command after I deleted all the log4j jars in the project and in the Tomcat libraries.
And even after deleting the log4j jars, I can still import the org.apache.log4j.Level class in any class in my project (and of course that Level class that I can import doesn't have the TRACE field).
So, how do I find where it is? And how does it get included in my project classpath???
Thank you for your time!
Try following to find location of the jar causing the issue:
System.out.println(org.apache.log4j.Level.class.getProtectionDomain().getCodeSource().getLocation());
The jvm argument '-verbose' or '-verbose:class' will output on the console for each class where it is loaded from (in most cases which jar). Possibly this is available to you (depending on how you run the webapp).
For log4j keep in mind that many people ship log4j with their jar. Ideally they should change the package name if they do that, but some don't. I wrote/assembled the following bash script to find which jar I used had log4j in it:
for file in *.jar
do
unzip -l "$file" 2> /dev/null | grep "log4j.dtd" && echo $
done
This looks for log4j.dtd which I was looking for (feel free to replace with any log4j class) and then prints the path in the zip file and after that the name of the zip file that contains it. Just run it from the directory with all your jars.

Categories

Resources