Java FileNotFoundException in jar file - java

I’m working on a background Spring service that runs from the command line using a nohup command.
I’m hitting the following error:
java.io.FileNotFoundException: class path resource [templates/] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/gestes/Documents/workspace/bge/bge-proj/myProcess/target/myProcess-0.2.2.jar!/templates/
The jar file is being created and does exist at:
/Users/gestes/Documents/workspace/bge/bge-proj/myProcess/target/myProcess-0.2.2.jar
When I extract the jar file contents, there is a /templates/ directory.
Looking at the exception, there is an “ ! “ after the jar file name, and I thought that tells what it can’t find, but clearly, it is there.
What am I missing?

If in your code you are trying to access the folder using a java File, you cannot do that. You need to use an inputStream
This is because things inside a Jar are not actually files on the disk. They are compiled inside of a jar. Yes it may be there when you extract the jar, but it's not actually a normal disk file when the jar is bundled

Related

Find file in external jar not in my classpath

I´m running some test and I need to get the path of a file that it´s in a jar lib that I have in my project as dependency.
This jar is not part of the classpath that I run.
If I try something like
val path = getClass.getResource("h2-1.3.161.jar").getPath
in my test it does not work.
Any idea how to find a file inside a jar without be this jar part of your classpath?
Regards.
Using the solution of astrograph I manage to get this route
java -cp //file:/D:/Users/nb38tv/workspace/f2e-core/f2e-mock/f2e-test-framework/target/f2e-test-framework-1.8.3-SNAPSHOT.jar!/h2/sakila-h2-master/h2-1.3.161.jar -ifExists -tcp -web -tcpAllowOthers
But java complain since cannot find the jar.
If I remove the ! from the path I receive this error
Unrecognized option: -ifExists
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Any idea?
Regards
Is the file a .class file?
Can you open the file in your test?
Can you instantiate a class from that jar file?
To get to the location of a class you can use the following method:
System.out.println(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
It depends on what you mean by "find" the file.
If you mean "detect if the class file exists", then you can use the Java Zip file handling routines. Inside your code, open the Jar file as a Zip file.
If you mean "use the class file" within your program, and for some reason you cannot put it on your class file (perhaps it doesn't exist in that location at startup time) then you need to use an additional class loader which will look for the file after the file is present. To do this, I recommend you reuse at URLClassLoader, even if your file is local to the disk, just use a file:/// URL.

What is the proper way to deploy Java project that has JSON files?

I am trying to deploy the program by making it into an executable jar file.
When I run the program directly from IDE (IntelliJ), everything is fine. But after I make jar file out of my project, when I run that jar file, I get errors like:
java.io.FileNotFoundException: src\JsonFiles\Words.json (The system cannot find the path specified)
Don't use relative paths to src folder, open an input stream to the file where it's located relative to a class:
InputStream in = SomeClass.class.getResourceAsStream("Words.json");
where Words.json and SomeClass.java exists in the same folder/package.

executable jar file not running outside of the folder where I created it

I created a jar file in my working folder
jar -cfe MRMC.jar MRMC *.class *.jar DB statpack
and I am able to double click the jar file or run it using
java -jar MRMC.jar
But when I copy the jar file to another location, I can no longer run it. I got errors:
Exception in thread "main" java.lang.NullPointerExeption
it seems that the jar file did not find files in the resource folder DB above.
Thank you.
I see two possible reasons:
a) the content of the DB directory does not end up in the jar. Unzip the jar to check what is actually inside. Note: a jar is just a zipfile.
b) you are not referencing the files as a classpath resource, but as a File Resource. Check the methods you are using, if they are based on the classpath or on the file system.
See this question for the different ways to load a file: How should I load files into my Java application?

Root Directory Information Included in Compiled Jar File?

I have a simple 'Hello World' command line Java application. I build the .jar from the command line - no IDE is used. I find that if the root directory is different (i.e., /hello/ vs. /hello/with/more/dirs/), the final .jar files are not exactly the same. (I compared the two with a Unix diff utility). It appears that the root directory is encoded into the .jar file.
Can anyone verify that is true?
Is there anything else encoded in the .jar file, such as timestamps?
Is there any way to prevent the path from being encoded?
A jar file is just a zip file (plus potentially a manifest). It contains the file list exactly like a zip file does. There is nothing you can do to prevent this.
If you don't want you files inside a directory in the jar, package it from the directory where you files reside. If you're more familiar with zip and don't need a manifest, you can just use that instead too.
There's a good overview of what a jar file actually is in the JAR file article on Wikipedia.

Files not extracted from .jar file when run

I have updated my ant build.xml file to include a new file and a new folder. After creating the .jar I check if they exist in the jar by 'unzip\extract', and they are there.
But when executing the .jar neither the folder or the file gets extracted.
Am I missing a step?
Look into getResourceAsStream. It'll keep you from having to extract the files from the jar file. Unless that's your goal.
Your application should be able to use the file directly from within the jar, no need for extracting it. Or do you mean something else?
Are you doing something specific to extract the jar file? I ask because normally jar files are not extracted when executing them.
If you run "java -jar myJar.jar" or "java -cp myJar.jar com.example.MyMainClass" the jar files that is referenced will not be extracted. Java will load your classes and resources directly from the jar file without extracting it.
If you wrap your application up using One-JAR, you can specify an attribute in the Manifest file to extract files that you want (See the One-Jar-Expand manifest attribute).
As a bonus, you will also be able to wrap any dependent libraries along with your code, creating a single distributable jar.

Categories