getresource

getResource puts a leading / before the disk name using java 1.7 windows 7

时光毁灭记忆、已成空白 提交于 2019-12-06 17:22:27
问题 The following gives a leading slash before the disk name. How can I avoid that? String pngpath = getClass().getResource("/resources/lena.png").getPath(); System.out.println("pngpath = "+pngpath); Gives: pngpath = /C:/Users/jgrimsdale/Documents/NetBeansProjects/HelloCV/build/classes/resources/lena.png 回答1: Use: String pngpath = getClass().getResource("/resources/lena.png").getFile(); File file = new File(pngpath); System.out.println(file.getAbsolutePath()); 回答2: A constructor of File(uri) or

getResourceAsStream working in eclipse, but not when run as applet in browser

随声附和 提交于 2019-12-06 16:33:22
I have an applet that needs to load saved data from a local text file that is in the eclipse package with the .java files. For use in this questions its name is "saveData.txt". When I run the applet through eclipse, everything works perfectly. However, once I jar it up (making sure the txt file and .classpath files are included) and put it onto a website, the applet fails to load. I am getting an error of Unknown source on my InputStreamReader. I have included my code for loading and saving below. The error comes on the creation of the InputStreamReader Line. Any Ideas where I screwed up?

getResource with parent directory reference

夙愿已清 提交于 2019-12-05 11:34:26
I have a java app where I'm trying to load a text file that will be included in the jar. When I do getClass().getResource("/a/b/c/") , it's able to create the URL for that path and I can print it out and everything looks fine. However, if I try getClass().getResource(/a/b/../") , then I get a null URL back. It seems to not like the .. in the path. Anyone see what I'm doing wrong? I can post more code if it would be helpful. The normalize() methods (there are four of them) in the FilenameUtils class could help you. It's in the Apache Commons IO library . final String name = "/a/b/../"; final

getResource puts a leading / before the disk name using java 1.7 windows 7

耗尽温柔 提交于 2019-12-04 23:01:47
The following gives a leading slash before the disk name. How can I avoid that? String pngpath = getClass().getResource("/resources/lena.png").getPath(); System.out.println("pngpath = "+pngpath); Gives: pngpath = /C:/Users/jgrimsdale/Documents/NetBeansProjects/HelloCV/build/classes/resources/lena.png Use: String pngpath = getClass().getResource("/resources/lena.png").getFile(); File file = new File(pngpath); System.out.println(file.getAbsolutePath()); A constructor of File(uri) or File(string) helps to get file object from system dependent path string or URI object. It is a solution to using

Java 9.0 | ClassLoader::getResourceAsStream: NullPointerException

老子叫甜甜 提交于 2019-12-03 17:31:39
问题 This piece of code down below, where I take my file from folder which is inside the " /resource " folder, works fine for me in Java 8: //e.g fileName = "folder0/file1.extension2" ClassLoader classLoader = ResourceLoader.class.getClassLoader(); InputStream in = classLoader.getResourceAsStream(fileName); Scanner scanner = new Scanner(in, "UTF-8"); In Java 9 it does not, classLoader.getResourceAsStream(fileName) returns null: java.lang.NullPointerException: source However, if I use files

getResourceEntryName for an array item

北战南征 提交于 2019-12-02 20:10:22
问题 I have a string array in strings.xml as detailed below: <string-array name="spinerArray"> <item name="one">First</item> <item name="two">Second</item> <item name="three">Third</item> <item name="four">Fourth</item> </string-array> And I need to retrieve one of the items name not the value (one, two, three, four). So far, I know it's possible to get the name of the array using getResourceEntryName like: getResources().getResourceEntryName(R.array.spinerArray); Is there a way to get an item

Java project with image files

馋奶兔 提交于 2019-12-02 18:30:05
问题 I am working on a project in which I use image files. Where is the proper location to store the folder with the images? Inside src folder (created by netbeans) or outside? Also when I clean and build the project a dist folder is created with the jar file in it. Can the images folder be included in the jar or I have to copy them in the same folder with the jar if I want to run the program on another computer? Finally, I would like to know how to use the getResource when I want to refer to the

Java project with image files

老子叫甜甜 提交于 2019-12-02 13:19:58
I am working on a project in which I use image files. Where is the proper location to store the folder with the images? Inside src folder (created by netbeans) or outside? Also when I clean and build the project a dist folder is created with the jar file in it. Can the images folder be included in the jar or I have to copy them in the same folder with the jar if I want to run the program on another computer? Finally, I would like to know how to use the getResource when I want to refer to the image folder from the source code. Thanks in advance. Where is the proper location to store the folder

getResourceEntryName for an array item

浪子不回头ぞ 提交于 2019-12-02 09:25:57
I have a string array in strings.xml as detailed below: <string-array name="spinerArray"> <item name="one">First</item> <item name="two">Second</item> <item name="three">Third</item> <item name="four">Fourth</item> </string-array> And I need to retrieve one of the items name not the value (one, two, three, four). So far, I know it's possible to get the name of the array using getResourceEntryName like: getResources().getResourceEntryName(R.array.spinerArray); Is there a way to get an item name? Thanks. As this is an xml, you will need a xml parser. Then you can get the values of name using

Getting File from .jar using .getResource()

感情迁移 提交于 2019-12-02 08:56:39
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(true); does paint the image after building. How can I acces the Image.jpg as a File object? The file is