getresource

Getting a resource in a different project using classloader

夙愿已清 提交于 2021-01-28 02:26:04
问题 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

Getting a resource in a different project using classloader

蓝咒 提交于 2021-01-27 22:51:07
问题 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

Get a resource using getResource()

ⅰ亾dé卋堺 提交于 2020-01-18 02:20:05
问题 I need to get a resource image file in a java project. What I'm doing is: URL url = TestGameTable.class.getClass(). getClassLoader().getResource("unibo.lsb.res/dice.jpg"); The directory structure is the following: unibo/ lsb/ res/ dice.jpg test/ ..../ /* other packages */ The fact is that I always get as the file doesn't exist. I have tried many different paths, but I couldn't solve the issue. Any hint? 回答1: TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg"); leading slash to denote

Get resources from another apk

我是研究僧i 提交于 2020-01-10 09:04:31
问题 I have been struggling with this issue all day and have had no success. I am basically trying to get an image resource from another apk. So if com.example.app has an image called image1.png in the res folder, i want com.example2.app to be able to gain access to that resource and place it in an imageview. I know you have to use PackageManager.getResourcesForApplication, but i have still been unsuccessful in getting the actual resource. Any help would be awesome! 回答1: Figured it out... final

java how to add image using getResource()?

我们两清 提交于 2020-01-06 13:13:13
问题 I want to add an image to a Jpanel Class, I use the method getResource() but it returns Exception in thread "main" java.lang.IllegalArgumentException: input == null! , because the path into the image folder that i give is not valid, now I want how to get the valid path, I'm aware that maybe it's really easy and it's not a question that I've to ask here but I'm spending a lot of time and trying different path but it doesn't work; here the Jpanel class import javax.imageio.ImageIO; import javax

Copying a binary file outside of a jar

时光总嘲笑我的痴心妄想 提交于 2020-01-04 09:37:22
问题 I have an exe packaged inside my jar file and I am trying to copy it to a temporary location so that i can run it using Desktop.browse() , to do this I set up a scanner with the input stream constructor using class.getResourceAsStream , and then with a printwriter wrote that all to a file. The problem that occurred says that the exe is invalid. I think this is due to some binary data being lost. If anyone can help please post a comment. Scanner sc = new Scanner(ClassBuilder.class

Copying a binary file outside of a jar

亡梦爱人 提交于 2020-01-04 09:37:02
问题 I have an exe packaged inside my jar file and I am trying to copy it to a temporary location so that i can run it using Desktop.browse() , to do this I set up a scanner with the input stream constructor using class.getResourceAsStream , and then with a printwriter wrote that all to a file. The problem that occurred says that the exe is invalid. I think this is due to some binary data being lost. If anyone can help please post a comment. Scanner sc = new Scanner(ClassBuilder.class

Why File.exists() does not work

时间秒杀一切 提交于 2020-01-03 05:37:27
问题 I have a file watstheday.txt in my src/main/resources folder of my project as shown in the image file below. I read the file through a getResourceAsStream() method of the ClassLoader and perform further actions in my code which is working perfectly. However if I try to perform a check if the file exists through the below code it always returns false. try { ClassLoader classLoader = getClass().getClassLoader(); System.out.println("!@#!@# so difficult to be simple..."+classLoader.getResource(

Getting File from .jar using .getResource()

六眼飞鱼酱① 提交于 2019-12-31 06:47:48
问题 I'm trying to acces a File inside the .jar. In Netbeans, this: System.out.println(new File(this.getClass().getResource("Image.jpg").getFile()).exists()); Prints out: true After building however, it prints false. The file is definitely in the .jar, next to the .class file, and something like this: new Frame(){ @Override public void paint(Graphics g){ try{ g.drawImage(ImageIO.read(this.getClass().getResource("Image.jpg")), 0, 0, this); } catch(Exception e){e.printStackTrace();} } }.setVisible

Junit + getResourceAsStream Returning Null

不想你离开。 提交于 2019-12-29 04:27:27
问题 Not sure how this is possible. I re-read up on getResourceAsStream and it's always returning null. InputStream source = this.getClass().getResourceAsStream("test.xml"); Right next to test.java in the Finder (using OS X and Eclipse) is test.xml I can open it in TextWrangler and view it as existing with data inside. This is a Junit test if it makes any difference. I went and looked at existing Junit tests on our system and I'm using it in the exactly same manner as a working example (as in