i need a help understanding what is happening in my project, anyways i have javafx with springboot:
i have folder structure like this :
So i want to access from my java/controller folder, and i need my css file that is under resources folder, in css folder.
I tried every possible way, to load my css and nothing seems to work except :
Directly loading css through fxml (which i wish to evade)
using this code also works, but only in IDE, if i create JAR it does not work anymore :
File f = new File("src/main/resources/css/main.css");
paneRoot.getStylesheets().add("file:///" + f.getAbsolutePath().replace("\\", "/"));
When i do check of f.exists() i get true, so path is ok, but when i use this :
paneRoot.getStylesheets().add(getClass().getResource("src/main/resources/css/main.css").toExternalForm());
or
paneRoot.getStylesheets().add("src/main/resources/css/main.css");
it does not work,in first code line, where i use .toExternalForm(), program crashes reporting nullPointer.
Now i honestly do not know what is issues here, i tried with 2 different IDE-s, i tried clearing cashe, rebulding and cleaning application, but every time same issue.
If i need to provide further code, i will i just need to understand this.
Related
I've been trying to export my project as a Runnable Jar, but my resources would not load because I was directly trying to access the image from my project's path. For example:
ImageIcon icon = new ImageIcon("resources/icon.png");
This would work when I ran the project from Eclipse itself, but I noticed that the images were not being included in the jar file. So after researching, I found out that I need to create source folders and put the images/text files inside them, and then use getClass().getResource() in order to access them. However, when I do this, the URL is always returned as null.
For reference, this is what my project explorer looks like:
Test
---src
---resources
---icon.png
---config
---file.ini
And here is the code that is giving me a NullPointerException when trying to access icon.png:
ImageIcon icon = getClass().getResource("/resources/icon.png");
Alternatively, I have also tried:
ImageIcon icon = getClass().getClassLoader().getResource("resources/icon.png");
But that also ends up in a NullPointerException. I have checked many solutions online but none of them have seemed to work for me. Please note that I also need to be able to access the .ini file, so a solution that only works for images won't fully solve my problem. Any help would be greatly appreciated, thanks.
Your structure needs to be:
TEST
-SRC
-Resources
-icon.png
The way your code is right now is there is no image because your code is referencing /src/resources not test/resources
I have spent all last night (until 3am) and this morning researching, testing, refactoring, and attempting to debug this issue. I have a simple Java game in Netbeans and while it runs perfectly perfect within the IDE in either run or debug mode, once exported into a jar file it refuses to load any resources corrrectly. There are many similar questions to this such as this one regarding loading an ImageIcon and despite great effort none of these solutions work for my project. I am not using ImageIcons, only simple BufferedImages and wav sound files. I recently refactored to combine my BufferedImageLoader and Sound classes into one Resource class, which I then moved into the same package as all my resources even though it worked perfectly well in a separate code package before in the IDE, although it works in its new location as well, strictly within the IDE.
I'm rather irritated and flustered from this issue. The truly infuriating thing is that this project used to work with resources after being exported into a jar, and now it seems to have stopped working with no changes. The only real programmatic difference between back when it worked and now is that I didn't have or use sound files back then, but this error isn't related to the sound files, as it catches an exception (and generates an error dialog) just from first trying to load the art assets.
I've tried every possible solution I've found in my research to no avail. Hopefully a fresh set of eyes can reveal the error of my ways.
The offending line of code is
return ImageIO.read(Resource.class.getResource("/res/" + imageFileName));
whereas imageFileName is the parameter with values passed from method calls such as
blockSheet = Resource.loadImage("art_assets/platform.png");
The location of the Resource class seemed to have no bearing on this working within Netbeans. My res folder is inside src, next to the com class package beginning.
It throws an IllegalArgumentException: input == null! exception. After some testing it seems that Resource.class.getResource("/res/" + imageFileName) returns a null value, which makes no sense at all. Again, this works perfectly perfect within the IDE. I can change the jar file into a zip and look inside to see that all the resources are exactly where they should be with the correct names and the correct extensions.
Here is a zip file of my entire project. Any help is immensely appreciated. Thank you.
EDIT:
Some of the things I've already tried:
getResourceAsStream() instead of getResource()
classLoader() between Resource.class and getResource()
this.getClass() instead of Resource.class from a non-static context
I think this should help:
How to get the path of a running JAR file?
CodeSource codeSource = YourMainClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();
provided by Benny Neugebauer in the post.
so I am in the process of making a small application.
Right now, the project works fine. I am running it through an IDE. The problem comes about when trying to run the project as a jar - which is the end result. Right now, it fails to properly load the required files (classes and simple ASCII files).
The method I am using is one based off of:
final Enumeration<URL> paths = CLASS_LOADER.getResources("");
Where CLASS_LOADER is an instance of class.getClassLoader().
This works great when not inside a jar. Inside a jar though, it seems to fail horribly. For example, in the code above, paths would be empty.
I am assuming that the fault is that the files are all within a jar - the same jar to be precise.
The class path for the manifest file is blank at the moment.
If it helps, I have two tasks that require loading files.
I need to create a list of all files that are a subclass of
another class.
I need to load a list of language files (all of
which are in the same directory).
If you need anything else to help debug this problem or provide a solution - let me know. Thanks for reading this!
For ClassLoader.getResources() to work you need to feed a path relative to the jar root. If you want to search the jar then ClassLoader public API won't help you. You have to use custom code based on java.util.jar.JarFile, like the one here.
I've been developing for android using processing but have come to a halt when I wanted to retrieve a list of files within a given directory. Below is a screenshot of the code I have been trying.
I have tried different variations of this (such as getbaseContext().getAssets();) and nothing seems to work. Whenever the code tries to execute the list() part it has an error and there is nothing in 'fileNames'.
Am I missing anything? Is this a problem with processing?
Thanks
EDIT: The direction I am trying to access is "assets/Levels/" which I can see from my project view in eclipse.
I experienced a similar issue because I used folder names with a trailing / character.
So for others experiencing this issue, you need to make sure the folder name you use are just the folder name with no trailing / (so "movies" instead of "movies/")
You do not need any additional permissions in order to run the code that you have. I just compiled it on my device and it works fine. You mention you are attempting to list the files in a given directory. What you are doing is listing the files in the Levels folder, which should be a sub folder of assets. What you will get back is an array of all file names in that folder.
If you are actually looking to get a list of files from a given directory, it has nothing to do with the assets folder and I suggest you check out a tutorial on Android external and internal storage. The information directly on the android page was very helpful to me.
http://developer.android.com/guide/topics/data/data-storage.html
If that's not what you are looking for though, I suggest rewording your question.
So I got it to work!
Turns out it was a problem with where I was placing the 'String[] fileNames;' code before trying to put data into it. The below code works fine:
Thanks for the help!
*Note : Individual folder should not empty.. It will return zero for folder with no files. *
void getFolderDetais(String folder_name)
{
// pass folder_name empty to get detail of root that means assets folder details
String[] filelistInSubfolder = assetManager.list(folder_name);
Log.v("Floder Details ----- >", " Length --- "
+ filelistInSubfolder.length);
}
First of all, I am aware of Stack Overflow (and any competent forum-like website) policy of "search first, ask last", and, doing my homework, I searched various sources to find a solution to my issue. That said, I, failing to find any suitable answers, was left no choice but to ask this problem personally.
I have somewhat moderate programming skills, especially regarding the Java language. I am working on this 2D game with the default Java SE JDK. More specifically JDK 7u4. In this project, we have a class that manages most I/O operations. One of its methods returns the path to a file:
public static URL load(String resource) {
return ZM.class.getResource(resource);
}
Now, this method works fine when running the project on Netbeans (version 7.1). However, when building and cleaning the project, the resulting .jar file does not seem to agree with its creator. When running the .jar on command line, the JVM caught a NullPointerException. It seemed that the file was not being able to be read inside the .jar. Following my programmers instinct, I started debugging the project. My first attempt was to check whether the load method was the faulty member. I ran some tests and obtained a couple of interesting results:
When running the application on Netbeans and with "ZM.class" as the methods argument, it returned:
/D:/Projects/GeometryZombiesMayhem/build/classes/geometryzombiesmayhem/ZM.class
But when running it from the .jar file, it returned:
file:/D:/Projects/GeometryZombiesMayhem/dist/GeometryZombiesMayhem.jar!/geometryzombiesmayhem/ZM.class
Naturally, I tried removing the initial file: string from it. No effect. Then I tried taking the exclamation mark from [...].jar![...]. Again, nothing. I tried removing all the possible permutations from the path. No luck.
Testing the method against the very own .jar file worked okay. Now, when I try to access the inside of the file, it doesn't let me. On earlier versions of this project it worked just fine. I am not really sure of what is going on. Any help is welcome.
Thank you in advance,
Renato
When loading resources from a jar file, I've always used a classLoader. Everything seems to work the same whether you run from within the IDE, launch the executable jar file or run the program from a web site using JNLP.
Try loading the resource this way instead:
try {
ClassLoader cl = ZM.getClass().getClassLoader();
ImageIcon img = new ImageIcon(cl.getResource("images/programIcon.jpg"));
// do stuff with img.
}
catch(Exception failed) {
System.out.println(failed);
}
One more suggestion - you should create a separate folder for resources. In my example above, images is a folder inside of my src folder. This way it will automatically become part of the jar when I build it, but I am keeping resources separate from source code.
I suppose your problem is in loading an image from your jar file.
Here is how i do it
URL imageurl = Myclassanme.class.getResource("/test/Ergophobia.jpg");
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));
This way I can get the Ergophobia.jpg file inside 'test' package.