I tried to export a Java program from Eclipse to a .jar file, but I came across a problem. It ran fine, but for some reason it didn't find the text files it was supposed to get data from. If anyone could help with this, I would be very grateful.
Where are these text files ? If they're packaged within your .jar file, I would expect to be able to find them within the class hierarchy using ClassLoader.getResourceAsStream(), specifying the complete path relative to the classloader base.
If your program depends on internal text files, put them in classpath and package them in the jar. Find them with the Class.getResourceAsStream.
I guess we need some more information to help you solve this problem. Where are your text files located while debugging and developing your code? Make sure that they are accessible from the path you are running the jar in.
Related
I am Java NOOB (seriously, about two weeks) and using IntelliJ.
My homework is to make a classes, which can process given images(which is in "input" folder), and store them into the "output" folder.
I really have no idea where to put those two folders(input, output) in.
My question is, which place should I put them in, without changing given directory written in template code.
Screenshot of my problem
By default, IntelliJ reads files from the top level directory of your project. For you, that is HW6. Place the input folder there.
See also Reading files with Intellij idea IDE.
In addition, it is possible to specify the "working directory in IntelliJ". You can specify where files are loaded from from the Run Configurations:
I've been working on a project in java for a while now. I realized something terrible when trying to run a .jar of the project: It wasn't using relative paths anymore, instead it was searching in the "C:\Users\Username\..." directory for files.
I read that instead of using new File("..."); you should use getClass().getResourceAsStream("...");
In a lot of places I used things like new Sprite(new File("sprites\\sprite.png"));, so it would take a very long time to go in and replace all of them.
I was hoping that there was something else I could do instead.
Like, is there any way to change the behavior of the File class?
Any tips? Thanks.
EDIT:
I tried Duran Wesley Harris's idea of setting the user.dir property to the path of the folder with the .jar, which actually successfully changed where the files were being looked for. But the program still fails to load the files. I can actually copy the directory it's using into File Explorer and load the file that way, so it's mysterious to me why it can't load it in the program.
Note: The files are not inside the .jar, they are sitting next to it in the same folder. Also, the program works perfectly in the IDE (IntelliJ Idea).
Personally, I think you should take the time to refactor your code to use xx.getResourceAsStream() methods.
If you want to try and make a hacky solution you could try setting the "user.dir" property to the root of the jar with System.setProperty() or java -Duser.dir="" via command line.
I have a jar file that's running, but it needs an exe and a couple dlls with it to work.
To make it convenient for the user, I want to package the folder with the exe and the 2 dlls in the jar, and have it extract it when the jar is ran.
I've read answers like this one, and this one, but I still don't understand how to apply that code to what I need. I understand that a jar file is essentially a zip, but I don't know how I can get the path to the zip, and extract the folder I need from it.
I tried using the code posted here to just extract the exe for a start, but it looks like it's trying to extract the exe from the class (if that makes any sense?)
Does anyone have a code snipped they could share to show how to extract a folder with a certain name from the currently executing jar?
If you guys could help me out I would really appreciate it!
If you want to access the resources from a JAR that is on the classpath at runtime, you simply access the resources from the classpath. So no need to read it via the JAR API. Therefore your first link points to a valid solution.
It may be tricky to use the appropriate classloader. If your resource is in the same folder as a Java class file, this link may help you: How to access resources in JAR file?
URL url = getClass().getResource("path/to/img.png");
or
URL url = MyClass.class.getResource("path/to/img.png");
I think you could access the two files separately and store them to a file. There are also methods to open a stream to copy. Here is a similar question: getResourceAsStream returns null
Hello people of stackoverflow :)
Today i have come across a problem which i need some help fixing.
I want to get the contents of a folder from my computer then add that contents into a jar file replacing any existing files already in that jar file. I am attempting to do this from within a java program.
I already wrote a method that will extract the contents of the jar file but i came up that this will be a faster and much more efficient method for what i have to do.
Any help with this would be greatly appreciated.
have you tried looking into the docs http://docs.oracle.com/javase/tutorial/deployment/jar/update.html ?
If you want to do it from a Java application, you can consider Runtime.getRuntime().exec("your command")
I'm using Eclipse as IDE. I've imported a .class file from a folder and I'm trying to use getResourceAsStream() to load it and then define it as a class. But it won't work, it comes up as null.
I've placed it in a folder called resources. So I'm using the path "/resources/Test.class"
I've tried loading a .txt file and that work. However it doesn't seem to want to load my .class file since it just ends up being null .
Any ideas? Thanks in advance for any help!
Solved: Sorry I'm a bit fresh to Java and this IDE so it was a bit of a mixup. The way I called the function, it expected the file to be in the same exact folder as the class that was calling it. Importing it into my package, solved the issue.
Sorry for not thinking ahead a bit further before posting. Thanks to those who took their time though!
Those are some of the things that usually work. If they don't, please post a minimal test case for us to reproduce your issue.
Try ThisClassName.getClassLoader().getResourceAsStream().
Also, if the path is in your classpath, try just "Test.class" without the folder name.