Problems loading .class file with getResourceAsStream() - java

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.

Related

File Not found, is my package setup correctly

I should first mention that I'm still an undergraduate, so excuse "beginner" mistakes, I'm also using the intellij IDE.
It's entirely possible that I'm missing something rather obvious but why is this .txt file not found when it's in a source root. My package hierarchy looks to be fine.
Would appreciate if anyone could point me in the right direction, thanks in advance.
As you can see fileNotFoundException
Seems like your project is not on build path . On Intellij you can configure it as mentioned in this post:
How to add a project to build path in IntelliJ Idea
And then you can access it using absolute path :
getClass().getResourceAsStream("/com/path/to/file.txt");
Or relative CLASSPATH path (when the class you are writing is in the same Java package as the resource file itself):
getClass().getResourceAsStream("file.txt");
I would also suggest you to read here:
https://www.mkyong.com/java/java-read-a-file-from-resources-folder/

How to make File class use relative paths?

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.

Exporting a JAR file in eclipse, seems fine but one of my classes does not work

I'm trying to export a project of mine containing four classes to a jar file (not executable) using the eclipse export method. everything seems to go fine, all four classes show up in the referenced libraries once I have added them to another program.
However, when importing the classes back into a program, I can only see three of them. One of them (the same one each time) does not show up to be imported. Any suggestions as to what might be causing this or how to fix it?
I have tried cleaning and rebuilding the project before I export it, but this doesn't help.
Thanks!
EDIT: Solution has been added to answers, apologies but I discovered the issue.
I apologise everybody but i discovered the solution. The class that was giving me issues had lost it's public modifier at somepoint, probably during refactoring. Once i'd added it back, the jar file behaved properly. Sorry about that.

Why is there no manifest.mf in the “dist/” directory and why is jar file not working?

I'm new to NetBeans and I cannot find the MANIFEST.MF that this link tells me there should be when I click "Clean and Build" in the IDE.
However, I only see a .jar file and a README.TXT. Anyone have any idea about this?
I am completely new to NetBeans, so simple terms would be much appreciated!
Also, when I run my project in the IDE, it creates and displays a jFrame but when I double click the jar file, nothing shows up. I have done setVisible() and I have set the default close operation, so that is not the problem. When I was searching for an answer, I found out that this could be because I did not specify my main class in my manifest. So this may be tied in with the top question so that is why I am combining these two questions.
Thanks in advance, guys!
I am using NetBeans 7.2 Beta
I realised that the manifest.mf file is not located in the "dist/" directory but in the root directory of the project. I am able to locate it now. However, I am still unable to figure out why the jar was not working. It is working now. Maybe my machine was just acting up or maybe I was using up too much RAM so my computer did not want to run another program? Anyway, everything is solved now. Thanks for everyone to answered!
Extract the .jar file using WinRar and then check inside the META-INF folder for MANIFEST.MF.

Java Export - Text Files

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.

Categories

Resources