Ressources can't be found in maven build - java

I wanted to migrate my local prototype to a maven build and start getting productive. But unfortunately when calling
loader.setLocation(getClass().getResource("views/TaskWindow.fxml"));
On runtime it seems that maven doenst find the ressource xmls.
I tryed "TaksWindow.xml", "../views/TaksWindow.xml" "views/TaskWindow.xml" and "/views/TaksWindow.xml" but somehow it always gives me "no location set" error.
My project structure looks like this:
Any ideas why?

The path you specified is a relative path. Which means the classloader will look for a views folder at the location of the class (i. e. its package). You propably want to use an absolute path.
Try the following code, notice the leading /.
getClass().getResource("/views/TaskWindow.fxml")
For more information, see this answer.

Related

Java getResource is in wrong path

I hope someone can help me here, becouse I'm fighting with a problem for some time. In my main class I use this command:
System.out.println(getClass().getClassLoader().getResource("org"));
The problem I've got is that it returns:
file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main/org
instead of:
file:/E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main/org
The problem is that it goes into classes directory instead of resources dir. As a result I can't have access to my .fxml files I need. I'm using gradle for build and currently working with JavaFX. I've tried something like:
System.out.println(getClass().getClassLoader().getResource("/resources/java/main/org"));
But I just got null :(
Do you know any method to force him to use absolute path or to look for resources in resource filder or even use something like to use "../" from linux to go up. I dodn;t find any of this
The root of your resources tree is defined by the classloader (as described in the JavaDoc). You can define the root by explicitely setting it in your classpath or preferably by using a build tool like maven and following the conventions set and used by the tool. For maven projects the root would usually be at main/java/resources.
getResource will always return the first match in the class path. So if you specify E:/Tmp/ExamplePr/PROJEKT/proj/build/resources/java/main before E:/Tmp/ExamplePr/PROJEKT/proj/build/classes/java/main in your classpath, you will get what you want.
That said, the resources are usually meant to be copied with the classes, and sometimes both are packed in a jar file, so you shouldn't worry about it.
With JavaFX use FXMLLoader;
FXMLLoader.load(new URL(getClass().getResource("/fxml/myfxml.fxml").toExternalForm()));
Make sure to pass the platform appropriate separator and use a relative path.

Same code behaving differently on two different packaging

I have a set of codes which are common for two different products, but a part of the code is behaving differently for each product
URL wsdlURL = IntegrationLandscapeService.class.getClassLoader().getResource("IntegrationLandscapeService.wsdl");
For One set up it is giving absolute path and for the other its giving relative path and this is leading to problems, my requirement is to get absolute path for the issue.
Packaging:
Code flow:
The call is made from the Main Class File and from there CommonCode in common code it is looking for the WSDL located in LandScapeService.jar
Update:
The next line of the code is
IntegrationLandscapeService landscapeService = new IntegrationLandscapeService(wsdlURL);
but I get the below error
failed [Failed to access the WSDL at: jar:file:/
tracker.jar!/lib/t24-IF_IntegrationLandscapeService-IntegrationLandscapeService-jwc.jar!/IntegrationLandscapeService.wsdl
.It failed with:
\tracker.jar (The system cannot find the file specified).]
Screen Shot of Jar
The error shows two '!' in the path which indicates the resource is in an embedded/nested/inner jar-file. A product that uses the fat/bundled-jar approach (where one jar-file contains other jar-files) will need some trickery to load classes and resources from the embedded jar-files. You can read about it at http://www.jdotsoft.com/JarClassLoader.php (*)
In any case, there is not much you can do about it since loading resources from embedded jars is not supported natively by Java. The implementation providing the "trickery" I mentioned above will need to fix that (and some do it better than others, see the link above).
The other product with a Par-file indicates the use of OSGi which only requires proper configuration to keep resource-loading working. There is an answer here that explains your situation and solution options.
(*) Spring-boot also has some good documentation and a solution, but I have not tried using the solution with a non-Spring application. See https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html for more information.
You can use getAbsolutePath
File file = new File(url.getPath());
String path = file.getAbsolutePath();
Isn't that what you are looking for?
This is because the files inside the JAR are not treated as regular files, as they are not expanded and not available directly to file explorer.
In Jar, if the files are referred, you will get the path from the root of the JAR file.
Please make sure you have the classpath entry for the Jar location. This will help to find the resource. Otherwise, try the following code(not sure whether it will work in your case, give it a try).
String resource = "/com/example/IntegrationLandscapeService.wsdl"; //package structure of the file
URL res = IntegrationLandscapeService.class.getResource(resource);
Refer this answer to understand more Stack Overflow comment

Class seems to be in Classpath, but still NoClassDefFound error (eclipse)

I am getting the java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException error, but the class does seem to be in the classpath.
Since it expects XMLParseException to be in net/n3, I have added the net/n3/... folder instead of just the XMLParser because I assumed it would be looking for that hierarchy. (Although I tried it both ways, and neither worked.)
Here is the 'Referenced Libraries' sidebar:
And here is my Run Config:
Thanks for any help.
At runtime, Java will look for the class in net/n3/nanoxml/XMLParseException.class relative to the entries in the Java Build Path, whether that's a jar file or directory. So, the "net" folder would need to be underneath the entry in the Build Path, not itself be an entry in the Build Path.
It seems there are space in the path(Programming Projects should be ProgrammingProjects), and some times space will create issues. Please removing the spaces in path and give it a try!

Intellij can't find .png

My intellij can't find local .png images.
private String craft = "craft.png";
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
The .png is located in the same directory as the java files. I don't understand why it isn't working. Using maven build, tried alternating from resources to java, but still no luck :(
craft.png must be placed into src/main/resources, otherwise it will be not copied to the classpath according to the Maven rules. See this answer for more details.
Your code should be also changed to:
private String craft = "/craft.png";
Here is the sample working project.
Go to your IntelliJ Preferences and search for "resource patterns" (or just go straight to the "Compiler" settings).
IntelliJ will only copy certain resources to the output directory. Make sure the resource pattern includes *.png.
I have my resource pattern set to !*.java (copy everything that's not a source file) which seems to work fine (and should really be the default, in my opinion).
tried alternating from resources to java
So at first you tried putting craft.png into src/main/resources. That is where it must be put according to Maven (not in src/main/java).
But it didn't work because
this.getClass().getResource("craft.png") tries to find "craft.png" relative to the this.class's package. If your this.class is in package foo.bar then you must put craft.png in src/main/resources/foo/bar/
You can also provide an absolute path in getResource() by using a leading slash /. For example put craft.png into a custom folder under resources src/main/resources/customFolder/ and read it with the leading slash / in front of customFolder:
this.getClass().getResource("/customFolder/craft.png")
If you don't use leading slash in getResource() method then internally class's package name is prepended to the resource name to make it absolute.
This behavior is explained in Class.getResource()

Getting a resource in a different project using classloader

Using the ClassLoader#getResource(), I need to access a file that is present in a project other than the one where my current code resides. How can this be done?
I'm using eclipse.
Directory Structure:
Root
|-project1
| |-package
| |-myResourceFile
|-project2
|-package
|-myCodeFile
I'm trying to get myResourceFile from myCodeFile, using myCodeFile.class.getClassLoader().getResource("../../project1/package/myResourceFile") but its always returning null. I do not want to add project1 to the classpath of project2. Though adding that also did not work.
With regards,
It's a bad idea to attempt to read files from another project like that because it ties you to exactly that directory structure. You already did the first step in decoupling the projects by using getResource() instead of using the java.util.File API so you can go the full way as well.
In Eclipse you can add other projects to a projects' build path (Project Properties -> Java Build Path -> Projects). You should be able to read the other projects' files now.
If you are using maven, then you can specify project1/package as a resource folder in the pom.xml of project2. You can theen use Classloader getResource method to get the resouce
http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

Categories

Resources