How import a file in a jar? - java

I have this project that it has this structure
Home
graphics
field.txt
example.java
I need to load field.txt in my example.java in jar and I use:
getClass().getClassLoader().getResource("field.txt").toUri();
but this code it give me "Null Pointer exception" .Anyone can help me?

example.class.getResource(“/graphics/field.txt“);
The class should belong to the same jar. For Class.getResource a relative “field.txt“ is possible (same package). With ClassLoader an absolute path for all classpaths is sought: “graphics/field.txt“.
To immediately read (never write) use getResourceAsStream or the URI of the getResource. One can use a Path on the URI. Files.copy(...).
One cannot at least should not) write a resource file (as it can reside in a jar jar:file://...; and jar might even be sealed; and resources might be cached by the jvm). Keep it as read-only template. Never File.
One technique is to create an application named directory in System.getProperty("user.home") and copy the template there.

To read the file it must be in classpath, you can put the file in the folder containing .class files or add it to the classpath with java -cp option.

The issue is not so much your code, but how you build and package your jar file. You will have to clarify how you are currently building your jar (using ant, maven, eclipse, etc ?).
Many articles will also advise you to separate out your resources from your source code (.java), and many IDE will support this separation direclty by allowing you to mark a folder as a resource folder. Even maven will allow you to customize this.
See following articles:
How to package resources in Jar properly
Using maven and netbeans, it is real simple: https://coderwall.com/p/d_cvrq/how-to-package-non-java-code-files-resources-in-a-jar-with-maven, or
use maven to pack javascript files in Jar?

Related

passing jar file in java code

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.

How to properly get resource Java project

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.

How to put a jar file in memory into classpath when compiling with jdk1.6 compiler API?

I'm doing some dynamic compilation using jdk1.6 compiler API.
For now, I just dumped all jar files needed to disk , in order to include them in classpath when compiling.
Is there some kind of trick to put jar files in memory into classpath directly?
I failed to figure out how to do this by extending javax.tools.ForwardingJavaFileManager.
Could someone give me some hints?
Thanks.
Read here on how to add jar files to your class path using manifest.
There should be a META-INF folder inside of your jar, the Manifest.mf file should be in that folder of the jar.
There is also an option to put the jar files in JAVA_HOME/lib/ext but this is a bad practice. The JRE can be used by many other applications than yours, and they don't need to have your classes in their classpath.
I wrote my own FileManager which is entirely in memory. Using this approach to can load and even write results into an in-memory "file system"
https://github.com/OpenHFT/Java-Runtime-Compiler/blob/master/compiler/src/main/java/net/openhft/compiler/MyJavaFileManager.java

Netbeans Clean & build classes

When I clean and build a project in NetBeans, the .jar file appears in the dist folder, like it's supposed to. But what if I have multiple files under the project? What happens to those files? E.g. I have a Game project, and under it are the different characters(knight, rogue, etc.) but I only see a game.jar file when I clean and build, I want to know what happens to the individual files. Thanks
Those files should be in the jar file as compiled .class files. It's easy to double check what's in the jar file since it's in zip format. You can use a program like 7-Zip to open it, or rename it to the zip extension (e.g. from mygame.jar to mygame.zip) and whatever OS you're using probably has some way to open it.
When you open or extract the jar file you'll find the compiled class files in a directory structure that reflects your package structure. For example, if you have Knight.java in the directory src/game/characters/Knight.java in the jar file you'll find something like classes/game/characters/Knight.class.
The name "jar" is an abbreviation of "Java archive". It stores all the classes and other resources (for example, images) in a project.
The classes you have defined in .java files will be compiled into .class files - these are contained in the .jar file.
All resources get compiled into the JAR file. If you want a separate JAR for the resources, you'll need to split the project into two maven projects: one jar for the code, one for the resources. You can then create a third project that would generate a distribution.
That's a lot of work, though. It's.a lot easier tO keep everything in one JAR unless you have explicit dynamic loading requirements.

Change the package of a JAR from the default package

I've downloaded a JAR file from my teacher's website containing some classes in the default package, and I'm thus unable to access them from inside a defined package.
I have read that the preferable solution is to repackage the JAR, changing the package name. However I have no idea how to go at it. The solution probably involves using Ant or Jar Jar, but I've no experience with either tool. I would love if someone coould point me in the right direction.
Thanks.
You need to change the sources and recompile then to change the package - simply moving the class files inside the jar (or outside) does not help.
So ask your teacher to give you the sources (or to put the classes in a suitable package), or use a decompiler to do this yourself.
You can unjar/unzip them manually, create the package and jar them back using and IDE or from the command prompt like this. Also, take a look at the ANT documentation on Jar and Unjar which is quite comprehensive.
As #Piyush Instructed use the below command for creating a Jar file.
jar -cvf *.* Example.jar
If you are using eclipse, just unjar the source files into the source folder of a temporary project. Then, create a new project (the real project you will be working on), and under the java/src directory, create the package structure you want. Then it's just a simple matter of drag-n-dropping the source files from the temporary project into the correct packages in the real project. Eclipse will take care of changing the package declaration of each class for you.

Categories