I have an app which has to read from a text file (using FileInputStream). The text file is in the directory structure relative to the class file (eg. "../textdir/text.txt"). When I run it normally (ie specifying the /bin folder containing the .class file in the cp) everything works fine. However, I somehow need to package everything into one jar and when I run the jar nothing works. The error is something like "FileNotFOund: MyJar.jar!/textdir/text.txt". I ran jar -tvf on the jarfile and the text file was indeed inside. I have read but not write access to the source code.
More than trying to solve my problem (I think there are plenty of workarounds), can someone explain to me how the whole thing work? How does the jar search for files? What if I want to read from current working directory of the command prompt instead of the directory of the .class in the jar file? Also, I recently had a similar problem with loading resources when I converted a non-jar project to a jar, how does that work?
Instead of opening the file as a FileInputStream, use getResourceAsStream which will work in both of your contexts ie. within the jar file or unpacked.
Related
I have a java maven project, which converts .json files to .yaml and vice versa. Long story short,I wrote method which creates resulting directory of converting process using path of where jar actually is. Then I created the method which creates files and writes there result of converting process to the /path_of_resulting_dir/file-name.json/.yaml. In case I run jar from my project in IntelliJ idea - everything is ok. However, when I run jar file from random folder on my desktop, nothing happens.
So, how can I correctly make jar to know if there are any files I should convert placed next to the jar file(e.g. C:\path_to_jar\SomeJarDir\jar.jar So files are in the SomeJarDir with a jar), or if there are any files which I access from path I write as jar argument need to be converted.
Link to the GitHub, where is my code placed(old version where I haven’t tried to make jar to know what to convert from any folder it’s placed, which works only if u run jar in IntelliJ idea):
https://github.com/foreverdumb/javaSprSum2021/tree/JavaSprSmrHmwrk/michaelProject
P.S. How can I force log4j to create log file next to jar?
your program maybe using relative path, so when you run your program from different location, it will read the directory relative to itself. Either provide the path during runtime as user input or set the absolute path to the jar.
I hava a jar file file.jar that needs a folder with pictures next to where the jar is. The program loads the pictures and shows them.
It works fine doing double click or java -jar file.jar, but if I try to open it with another java program doing
File file = new File("path/file.jar");
Desktop.getDesktop().open(file)
it´s not loading the pictures.
What can it be?
When you run java -jar file.jar, the jar file is in the current directory, and presumably so is the picture folder.
When you double-click a jar file, the code is run with the containing folder as the current directory.
When you "open" path/file.jar, the current directory is obviously different, because why else would you need to qualify the jar file name. Since you program replies on the current directory to find the picture folder, it fails.
Solutions, in my recommendation order (given the little I know of your code):
Include the pictures inside the jar file, then access them using getResourceAsStream.
Include the pictures in another jar file, have the manifest file of you main jar file include the picture jar file in the classpath.
Make sure the current directory is correct before try to "open" the file.
I've been wanting to make executable jar files with java lately. When executing my code with Eclipse it works perfectly. But when I use Eclipse to export the same code as a runnable jar, Most of my jars work except the ones that draw from separate source folders.
The jar will be made but when launched it will try and open and then just say to check to console for possible errors. I try and run the jar through the console with the command "java -jar test.jar". and It says it cannot access the jar. Any Ideas? Btw Im on a macbook pro osX. Thank you!!
picture of where my files are within eclipse
If you have a file you want to store in a jar and access from there, you don't really have a Java File any more. Look at Class.getResourceAsStream() and Class.getResource() - the first can give you an InputStream to the (used-to-be) file, the second returns a URL and can be used for things like images. Note that the file being accessed can be accessed relative to the package/folder location of the class or relative to a classpath root (by putting "/" at the front of the resource name ("/resource/funny.jpg")).
When you execute the jar from a command line, be aware that you have a thing called the "default directory"; it is a folder in which your commands execute by default. If your jar is not in the default directory, you have to specify a valid folder path to your jar to execute it.
I'm trying to read from a file that is packaged up inside a JAR, along with the class that reads it. To do this, I use the following:
getClass().getClassLoader().getResourceAsStream(file)
This works fine when I create and run the JAR file on OSX, but if I create and run the JAR file on windows, the above line returns null.
Am I missing something here? If I create the JAR on OSX and run it on Windows it works fine. The problem only occurs when I create the JAR on windows.
EDIT: It's worth mentioning that there is no folder hierarchy within the JAR file. Everything is stored at one level, thus the class reading the file and the file itself are both in the same directory. Additionally, this is how I'm creating the JAR file, on both OSX and Windows:
jar -cmf manifest.mf run.jar *.class file1 file2
EDIT 2: The file I am trying to load is a java .properties file. I take it that's not what is causing the issue?
Skip the classloader part. Just getClass().getResource....
Try it this way getClass().getResourceAsStream("/file1").
When using file separators, don't hard code them!
Use java.io.File.separator instead: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#separator
I have a java class that uses a bat file to execute commands. However I developed it in Eclipse IDE. It works fine in there. But as I export it in a jar file, it fails to find the bat file that was included.(gives me an IOException)
The file structure in eclipse is as follows
:
Project1
---->src
------>com.myproj
-------->BatFileRead.java
----md.bat
----ul.bat
md.bat and ul.bat is same level as src directory. After jarring it src folder disappears.
Could someone help me with this.
Thanks
In order to execute the command, you'll have to extract the bat file afterwards. You can't run executables which are inside jar files. Basically you'll need to open the batch file entry in the jar file as an input stream, and copy the data to a FileOutputStream on disk. You won't be able to execute it until it's a proper standalone file on the file system.
If you're already trying to extract it, chances are you're using getResource or getResourceAsStream slightly incorrectly. This is easy to do, because it depends whether you're calling ClassLoader.getResourceAsStream or Class.getResourceAsStream. The first only ever uses absolute paths (implicitly) and the second can use either absolute or relative paths. For example, in your case you'd want:
BatFileRead.class.getResourceAsStream("/md.bat")
or
BatFileRead.class.getClassLoader().getResourceAsStream("md.bat")
Have you checked that the bat files are definitely ending up in the jar file? Just list the contents with
jar tvf file.jar
to see what's in there.
Well this can be very dangerous. Be sure to use gloves when dealing with the BAT. They bite and quite painfull. Also try getting jar that has a big enough opening, although the bats will fit almost through any hole.
Good luck, and don't try this at home. This is done by professionals.
Try copying file.jar to file.zip, and opening file.zip (e.g. just double clicking, or using e.g. 7-Zip). Can you find your .bat files inside? If the .bat file isn't there, you have to add the .bat file to your Eclipse project, and rebuild in Eclipse. If the .bat file is there, but you still get an IOException opening it from you .java application, then please post the code snippet triggering the IOException, and please give a full listing of file.zip (get the listing by extracting file.zip to C:\testdir, and running dir /s C:\testdir in the command line).
Please note that Jon Skeet is right that although it is possible to open any file in the .jar file as an InputStream using java.lang.Class.getResourceAsStream and java.lang.ClassLoader.getResourceAsStream (see tutorial or find them in the Java API docs), but in order to execute a .bat file inside, you have to extract the .jar file first.