Java SE Maven project using ResourceBundle for internationalization - java

I'm working on a Swing application that has to be localized.
For that purpose, I'm using a resourceBundle. It works fine when I run the application from the IDE. But when I use maven to generate the jar, the application starts up normally but the internationalization stops working. Without throwing any exception, I have make a lot of variations to solve that problem; I even have make a class that inherits from java.util.ResourceBundle.Control. The only thing I've achieved to make it work is by placing the properties files outside the package (I place it into the desktop just for testing). I then try to use the same method by specifying the path by new File(".").getAbsolutePath().
Doing that still keeps working from running it from Eclipse, but when I try to generate the jar and execute it, it throws an exception and the application does not start.

If you place your properties file into src/main/java next to the Java code, Maven will probably not pick it up.
The canonical location would be src/main/resources (with the package structure below that duplicated just as it is now in your code).

Related

get the root directory of a maven application

I need to provide a settings file for my program, to which the user should have access to write some of the settings i need.
I created a file under a new directory (called settings) on the root of my application, but i have problem finding it at run time.
I use
File SettingsFile=new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()+"settings/CreateSettings.txt");
When i execute this under eclipse i get
/application/home/dir/target/classes/settings/ZipCreateSettings.txt which is wrong.
If i execute it on terminal using java -jar, i get the correct path,
/application/home/dir/settings/ZipCreateSettings.txt
This would cause me problems cos i need to run the application directly from eclipse and not use the terminal, even though it is going to be executed using the jar when it is up and running.
I cant keep it like that anyway, cos this code might end up in someone else's hands, and they would have no idea what to do with it.
I have also used some other techniques like
new java.io.File("").getAbsolutePath(); but this always gives me the current working directory, so if i execute the jar from /home, i would get /home.
I think the problem might be maven (which i am not familiar with at all) since my code worked with a plain java application some time ago.
Since your file will be located outside your classpath, it is basically outside your application. Your application is not aware of files existing outside it's classpath. So you will need some kind of way to provide the Full/Absolute path to your file. You can't use the classloader in your case.
I suggest you use a system param instead of a hardcoded value. See here

How do I access Java resources in a Grails project?

Grails newbie here. My application is in Grails, my IDE is IntelliJ IDEA. I configured my project (in IntelliJ) to say that my resources folder is under root\src\resources. I have an image file in there that I need to load into an InputStream / BufferedImage. However I can't seem to get to those resources from my Grails controller. This is what I was trying:
def image = this.getClass().getClassLoader().getResourceAsStream("image.png")
But that just returns null. I use this exact same convention in Java projects (well, except I have to declare image being of type InputStream), and it does work there. I was under the impression that I could essentially drop Java code in a Grails project and it should just work. What do I need to do differently to get this to work in my Grails controller? I need to access that static resource file.
If you mark a directory as a source directory in IntelliJ IDEA, Grails won't know about it. You have to configure Grails properly by either adding your new directory as a source directory or move the resource to one of the standard source directories.
I've never actually added a new source directory myself, but the answer to this stackoverflow question looks promising.
Other than that, you can just add resources to any source directory and it will be included, for example: grails-app/conf, src/java, src/groovy and more. In addition, any file in web-app/META-INF/classes will also be in the classpath of the application. The last one is great to know about if you need to copy a java or groovy source file (i.e. just copy, no compilation).
Try this
servletContext.getResourceAsStream("/WEB-INF/resources/yourfile")

Class.forName(Class) in a specific directory?

While testing a program I am developing that writes to a SQLite database, I noticed that if I moved my sqlite.jar file to a sub-directory, Class.forName("org.sqlite.JDBC") throws the ClassNotFoundException. Is there a way that I can change the directory that Class.forName("org.sqlite.JDBC") is looking in so that I can store the file where I want it?
It looks like the classpath is set to work with that location. If you are using an IDE, add the library to your build path in your project properties and then execute your code. The IDE will do the rest of the things you need to do.
If you are working with classpath setup, you should not move your files from the classpath where the class files are specified and your program will look for its desired class to load.

NoClassDefFoundError in the simplest tomcat web app ever

I have an extremely simple web app for experimenting in java. I have the following java class defined:
UtilDate.java http://www.philbair.org/samples/UtilDate_java.jpg
And a JSP file as follows:
UtilDate.java http://www.philbair.org/samples/getdate_jsp.jpg
The tomcat folders/files look like this (this is not official syntax, just an abstract representation of the folder structure):
UtilDate.java http://www.philbair.org/samples/tree.jpg
mywebapp/WEB-INF/classes/com/udate/UtilDate.class
The jsp file is under the root 'mywebapp' folder (name is inconsequential), and all these are under the webapps folder in tomcat.
After compiling this java class (UtilDate.java), I restart tomcat to make sure it's not hanging on to any old cache stuff.
I get the following...
javax.servlet.ServletException: java.lang.NoClassDefFoundError: UtilDate
when I try to put the jsp page in the browser.
If I replace the uDate() method in the print statement of the jsp file with
out.print("hello");
I get the word hello on the browser output.
This is crude code for testing a simple web app. I am not looking for advice on best practices (although that advice is excellent from what I've seen on this site), I just want to know why this doesn't work. It worked once, and after recompiling the class, it stopped working. I can run the class on my command line and it works just fine (outputs the date as in the main method).
I'm running Windows 7, and tomcat 7.0.34. I'm not using eclipse, or netbeans, etc. at the moment. I'm using a simple text editor, compiling the java class with the command line javac, creating the web folders manually, and placing all the files where they belong myself instead of having maven or ant or anything else build it for me. No war file at the moment. Just working with the files right in the tomcat folders under webapps. I know there are more sophisticated and recommended ways of doing this, but it's only a test for my own understanding with nothing to shield me from what's going on.
Again, this worked once.
Thanks in advance for any help.
you forgot to add the statement package com.udate; at the top of your java class. Add and compile , you should be through

JAXB JSON JAR Runtime List reader not available

I have a small project created with IntelliJ IDEA, using Jersey.
I already created the server part working just fine and hosted on a GAE server.
I try to connect to it via a Jersey Client, and here comes the strange stuff.
Everything works fine if I run my application from inside the IntelliJ IDEA. However, once I run the jar JSON support seems to be gone, however XML works. Any ideas?
I feel I need to mention that the jersey-json is packaged with the generated jar.
Here it is the jar file in question. You'll need both the jar and the dll file, one near the other and just run it.
The Console Output (Error): https://docs.google.com/open?id=0B42XvjSlpDCtTTdwQl9MSTBlQ0U
The JAR file I used (all dependencies extracted into it). Any JRE6 may run it. https://docs.google.com/open?id=0B42XvjSlpDCtTVNUQTQ4SXFCcDg
The DLL required for the JAR (although, it should work without this too): https://docs.google.com/open?id=0B42XvjSlpDCtNXpabVk5RDkyRW8
And here's the source code on the github (public): https://github.com/gaborbernat/fs_viewer
Finally, I attach a picture with the settings of the artifact (JAR) generate rules.
Thanks for your support, :)
please see http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-tests/src/test/java/com/sun/jersey/impl/json/ListOfJAXBBeanTest.java?rev=5698
(your usecase can be extracted from there; the point is that you need to configure Jersey Client to be able to handle json by a) registering JAXBContextResolver or b) explicitly adding MessageBodyWriters (jackson))
And dont forget to use GenericEntity (as in linked test).

Categories