I have a Java project which uses a third party application. I have the license file (.lic format) stored in the resources folder. Upon running the Ant script, it will copy this file into the /lib/jar directory as it rolls up the project into a Jar file to use on the server. This is where I will need to access the file when running the system live. Here is how the folder structure looks
MyProject
src
package
AccessingClass.java
resources
File.lic
lib
jar
File.lic (upon copy from Ant)
I am not sure the best way to do this so any suggestions other than how I have been trying will probably be helpful. The 3rd party project has a method in a class like License.setLicense(), which can either take a String to the location or an InputStream of the file.
I have been playing around with feeding it an InputStream, but always get a null value when calling getClass().getResourceAsStream(). Here is everything I have tried:
getClass().getResourceAsStream("../../../lib/jar/File.lic");
getClass().getResourceAsStream("/File.lic");
And as a backup I also tried (for local builds I figure I would try the resource folder):
getClass().getResourceAsStream("../../../resources/File.lic");
getClass().getResourceAsStream("/File.lic");
Is there a better method to perform this action? Or would someone be able to tell me why what I am trying is failing? Thanks ahead of time.
Are you running this code standalone or in IDE env looks like classpath issue. If you are running at command prompt you have to set classpath to lib dir if in ide make sure you resources dir is in classpath.
First, you need to ensure that the JAR is added in your class path.
Below should work.
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("/resources/File.lic");
Assuming File.lic is placed in root folder of the jar.
Related
I have a function that requires the path of a jar file
builder.add(EventAnnotator.createAnnotatorDescription("/org/apache/ctakes/temporal/ae/eventannotator/model.jar"));
This refers to the jar file in my resource folder (as far as I can understand).
I have the same jar file in my maven local repo. and want to use it instead.
Is there a way to pass it as a string like this ?
well it seems we could directly use the path of the jar file, as per the structure of resources (if you put it there). Previously this wasn't working for me as I had a few other errors.
In my project i currently have a setup using eclipse
But when i try to load the file "bg.png" by calling
getClass().getResource("/res/bg.png") or getClass().getResourceAsStream("/res/bg.png") I get a NPE
Can anyone tell me whats happening here? I never really thought there was much difference between how both methods locate their files
TIA
getClass().getResource[AsStream]() uses the class loader to load resources: the same mechanism as the one used to load class files based on the classpath.
So, to be able to load the resource, it must be in a jar file or under a directory that is part of the classpath. That is not the case here.
Move the res directory to the src directory: the file will then be in an Eclipse source directory, and Eclipse will "compile" it by simply copying the file to its bin/classes/whatever destination directory, which is in the classpath when running the application.
Using eclipse, exporting a runnable jar file is pretty simple when I'm only using the application on my computer. Any files that the program is using (sprite sheets, audio tracks, etc.) only exist on my computer, so sending solely the jar to another machine won't work.
What is the easiest way to package a jar along with all the necessary files so that I could run the program on any machine?
I see from your tags that you are working in Eclipse. I am not sure if this method will work in other IDEs and I don't think it'll work at all if you're doing everything manually (it relies on the compiler automatically copying resources over to the bin folder.
The simplest way (at least what I use) is to define another sourcefolder (I like res).
Then you can just add packages to this source folder and dump the relevant images. Then rebuild your project.
Finally, you can use getClass().getResourceAsStream("package/path/file_name.whatever"); to get your files.
After an export as jar, it should work, even on other machines.
If you don't require the other files to be actual files on the file system (which means you can't use File, FileInputStream, etc) then you can use the resource system. If you put them inside the JAR, you can access them like this:
InputStream fileStream = SomeClassInYourJarFile.class
.getResourceAsStream("/path/to/file.png");
This example would give you an InputStream reading from the /path/to/file.png entry in your JAR file - that is, the "file.png" file inside a folder "to" inside a folder "path".
This does not require the files to be in a JAR file - it can load them from wherever your .class files are stored, JAR or not. If you put them in your source folder, Eclipse will automatically copy them to that place - so the above line would also work if you had a package path.to containing a file called file.png.
In my Maven project I have a properties file that has a property for a location of keystore file file=filename.p12 (I think the file type doesn't really matter now).
The problem I have that when i built it with maven, I see that the file is inside the root of jar and when i run java -jar the-jar-file.jar I get the IO exception that the filename.p12 is not found.
Everything runs fine in Eclipse, it finds the file and the application runs. Not to confuse somebody, I keep a copy of that filename.p12 as well in src/main/resources folder so that the paths are resolved running in Eclipse and standalone. But this is going to be my other question.
What I can't do is to get the filename.p12 as a resource, because I have external jar that gets as argument my properties file and then handles that properties file itself where the row file=filename.p12 is. Why is the file not found inside the jar, even though I see it's there? My other property files that I have open with Spring's ClassPathResource run just fine.
In order to access internal/embedded resources you need to use Class#getResource or Class#getResourceAsStream depending on your needs
I made my java project into an executable jar using the export to jar option in eclipse. The jar runs as expected, except that it does not use any of the serialized files. I can see that clearly from the GUI. What could be the reason for this problem and how do I fix it ?
I saw this related question - Why does my JAR file not create a serialization?
But it does not tell me exactly how to get around this problem. It looks like you cannot pack a folder into a jar. Why ? Because code could accidentally/intentionally continue to add data into that folder and make the whole jar occupy the hard disk ?
How do I create some kind of structure in which I pack my executable jar and its serialization folder ?
Answering this question:
How do I create some kind of structure in which I pack my executable jar and its serialization folder ?
A common approach is to have a well-defined place to store serialized files, settings, etc, that does not depend on where the program has been executed from. Usually it is user's home directory, or Application Data in case of windows. I used this code to store my application settings:
String home = System.getenv("APPDATA");
if (StringUtils.isEmpty(home)) {
home = System.getProperty("user.home");
}
CONFIG_HOME = new File(home, ".myProgram").getAbsoluteFile();
CONFIG_HOME.mkdirs();
So on windows it will use AppData and on *nix systems it will use user's home. The dot in front of myProgram is to make it hidden on *nix platforms, which is a common practice.
EDIT For your question in your comment:
on my linux machine there is no APPDATA env variable so this code will create a directory /home/myUser/.myProgram. On windows it will be something like c:/Users/myUser/AppData/Local/.myProgram. On MacOSX, no idea.
You need your JAR to use the same path for reading the Serialized Files as your code in eclipse.
So you make a properties file containing the directory with your serialized objects.
Then, this is the same for both your JAR and our project.
See also: http://www.mkyong.com/java/java-properties-file-examples/
You can use
AClass.class.getResource(String str);
//or
AClass.class.getResourceAsStream(String str);
AClass: one of your classes.
str: file location which you want to read.
For example;
if your class hierarchy seem like this:
+src
+-com
+-test
|-AClass.java
+-util
+-PrintUtil.java
+-resources
|-Bouble.png
|-Mouse.png
+-Ocean.png
and for reading "Mouse.png" image, you can this with a lots of ways:
AClass.class.getResource("/resources/Mouse.png");
//or
PrintUtil.class.getResource("../resources/Mouse.png");
...
You can't write inside a jar file while you are using/running the jar file. When you put the jar file in you classpath or you run the program from jar directly, the jar will be locked by your jvm, hence it won't allow you to update the same jar file which you are currently using.
The solution given by people which says use resource as stream will work if your classes are there in a folder, not in an archive (which you are using).
As an archive you can't directly update it, you need to do following steps (by yourself or by 3rd party api),
Extract in temp location
update the files
re archive
Now as the jar file is locked, you won't be able to do the third operation, which is not even safe. As an example when you are running a jar file, try to rename it, it won't happen, if it happens, the jar file is not yet locked by the jvm, it gets locked whenever you call a class which is inside the jar file.
For better and secure serialization and file saving please look into this: java.util.prefs.Preferences.