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
Related
I am working with Gradle and I am trying to update/copy resources from one project to another. If I try
[...]
//copies resource bundles from root project
from ("cfg/resources") {
into "cfg/resources"
}
[...]
My package got copied to the cfg folder as expected like:
What I actually want to achieve is to keep my project structure like:
Any hint?
Once I created a Source Folder in my project with the given path cfg/resources, it looked as I wanted. Even after deleting everything and copying again. I am using Eclipse by the way...
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.
So I have 2 projects A and B, A having a dependency on B in pom.xml . I have a file in A which wants to use a resource in B called C.wsdl. I use the following way to refer it:
wsdlLocation="classpath:/wsdl/C/C.wsdl"
I installed B then installed A using maven. I open A's target folder and find A.zip. I extract A.zip and find a lib folder containing B's jar file. I extract the jar which has a folder C containing C.wsdl.
But I get the following error
Can't find wsdl at classpath:/wsdl/QuerySubscriberInfoService/QuerySubscriberInfoService?wsdl
Also that works if the file is in A itself.
Any idea where Im going wrong with this?
Seemingly, the author of this topic is having similiar problems, please check
XSD and WSDL in different directories
If this is possible for you to initialize wsdlLocation dynamically,
you can use
ClassLoader.getSystemResource("wsdl/C/C.wsdl")
Please note that it is only possible to extract content of such files (which are packaged inside of dependant jar) only via streams, i.e
ClassLoader.getSystemResourceAsStream("wsdl/C/C.wsdl")
I have a Java project and have to load resources such as sounds or images, which worked pretty well until I exported it into a jar file, where the app crashed because I it couldn't access the resources. I found after some research that I should use getClass().getClassLoader().getResource() or Class.getResource(). But after trying all the possibilities with the first or second function, with or without the leading /, but each time, I get null as a result, and used res/images/bg.jpg as argument. My project hierarchy looks like this:
|src
|Main.java
|res
|images
|bg.jpg
None of the solutions I've found on Stack Overflow or on Google worked for me. What am I doing wrong and what should I use?
EDIT
When exporting as jar, I am just right clicking on my project on Eclipse (without using any plug-ins), export and select runnable jar and explicitly declare my class Main as Classpath. When checking the content of the jar file, I can see the resources in the correct places.
The res folder should be a child of your src folder
|src
|Main.java
|res
|images
|bg.jpg
although standard practice would be to have the layout closer to
|src
| main
|java
|package
|Main.java
|resources
|images
|bp.jpg
The last time something like this happened to me, it was because my build did not copy the files into the jar. Might be worth doing a sanity check by listing the files in your jar:
jar tf /path/to/your.jar
i want to access a class from another project in the workspace using ClassLoader. How can i specify the path to that class and get that class file ?I am using Eclipse . Help
You have to just add that project in to build path configuration of the project where you want to access that. And the class loader will find that class depending on the import statement you have given in the class where you are using it.
To add in the build path : you have to right click on the project > select Build Path > Configure Build Path > then select a project tab and add the project in which the class is present which you want load.
You could try URLClassLoader an example is here
I never used GNU Classpath which could be heplful.
While I'm sure there are devious ways of doing exactly that, I think having one project reference another this way is not the best of ideas.
What you can do is create a jar of the project you're getting the class from via Export->Java->JAR file and put that file into your project. This will let you access the class you need while still keeping your projects self-contained.