getresource

Access a resource outside a jar from the jar

こ雲淡風輕ζ 提交于 2019-12-28 06:47:31
问题 I'm trying to access a resource from a jar file. The resource is located in the same directory where is the jar. my-dir: tester.jar test.jpg I tried different things including the following, but every time the input stream is null: [1] String path = new File(".").getAbsolutePath(); InputStream inputStream = this.getClass().getResourceAsStream(path.replace("\\.", "\\") + "test.jpg"); [2] File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());

Access a resource outside a jar from the jar

人走茶凉 提交于 2019-12-28 06:47:19
问题 I'm trying to access a resource from a jar file. The resource is located in the same directory where is the jar. my-dir: tester.jar test.jpg I tried different things including the following, but every time the input stream is null: [1] String path = new File(".").getAbsolutePath(); InputStream inputStream = this.getClass().getResourceAsStream(path.replace("\\.", "\\") + "test.jpg"); [2] File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());

Localizing strings in strings.xml gives NullPointerException

↘锁芯ラ 提交于 2019-12-25 01:55:56
问题 My work computer that Eclipse is installed on does not have internet connectivity due to work related issues so all code and LogCat text has been hand typed instead of copy and pasted since I am on a separate laptop that Eclipse is installed right now. So bear with me for any typos. Now to the issue. In the new version of my app, I am making it Spanish supported. I localized all my strings in strings.xml . Below is my Java code that I am not usuing to implement. public class SplashScreen

Iterate through certain images in res/drawable-mdpi

走远了吗. 提交于 2019-12-23 04:35:34
问题 In the res/drawable-mdpi folder I have 26 letter images, named big_0.png to big_25.png . I would like to add them all (and ignore any other images in the folder) to a hash map: private static HashMap<Character, Drawable> sHash = new HashMap<Character, Drawable>(); Initially I was planning something like: private static final CharacterIterator ABC = new StringCharacterIterator("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); int i = 0; for (char c = ABC.first(); c != CharacterIterator.DONE; c = ABC.next(), i++)

Java class getResource() with eclipse

人走茶凉 提交于 2019-12-19 03:21:08
问题 i've been trying to get getResource to work correctly this whole morning but so far the only way i've had to get it working is by moving the res folder to the bin folder and then using getResource like so URL url = MyClass.class.getResource("/res/gfx/entity/entity1.png"); But is there not any way i can make it so res is outside the bin folder? 回答1: Nevermind, i can't believe how stupid of a fix that was. I added the res folder to the main project folder rather than bin, then went in eclipse

Java Swing: unable to load image using getResource

允我心安 提交于 2019-12-18 09:31:49
问题 I'm trying to isolate where the problem could be when trying to add an image to the class directory. (Doing this so when I export as a runnable JAR, the image is included in the package). So I've got the strawberry.jpg file sitting in 'C:\Users\sean\workspace\myApps\src\testing' Could you advise what I'm missing? Thanks! package testing; import java.awt.*; import javax.swing.*; public class IconTest { public static void main(String[] arguments) { JFrame frame1 = new JFrame(); frame1.setTitle(

Java Swing: unable to load image using getResource

六眼飞鱼酱① 提交于 2019-12-18 09:30:06
问题 I'm trying to isolate where the problem could be when trying to add an image to the class directory. (Doing this so when I export as a runnable JAR, the image is included in the package). So I've got the strawberry.jpg file sitting in 'C:\Users\sean\workspace\myApps\src\testing' Could you advise what I'm missing? Thanks! package testing; import java.awt.*; import javax.swing.*; public class IconTest { public static void main(String[] arguments) { JFrame frame1 = new JFrame(); frame1.setTitle(

Reading File In JAR using Relative Path

坚强是说给别人听的谎言 提交于 2019-12-17 23:26:33
问题 I have some text configuration file that need to be read by my program. My current code is: protected File getConfigFile() { URL url = getClass().getResource("wof.txt"); return new File(url.getFile().replaceAll("%20", " ")); } This works when I run it locally in eclipse, though I did have to do that hack to deal with the space in the path name. The config file is in the same package as the method above. However, when I export the application as a jar I am having problems with it. The jar

Getting location of file as string in resource folder

心不动则不痛 提交于 2019-12-14 03:14:42
问题 I have completed a program in eclipse and now I would like to export it as a single runnable jar file. The program contains a resource folder with images and text files in it. This is located beneath the source folder. The res file is not added to the build path however when I run the program in Eclipse it still works. The thing that is confusing me is that the res file is being saved into the runnable jar file when I export it as I can open the Jar file with WinRar and I see the folder is

Does ServletContext.getRealPath() work with web fragments?

孤者浪人 提交于 2019-12-12 05:33:35
问题 I use web fragments (servlet 3 spec) and thus can load e.g. META-INF/resources/access.xml file which is in a library in /WEB-INF/lib/ of my WAR via ServletContext.getResourceAsStream("access.xml") . Doing the same with ServletContext.getRealPath("access.xml") doesn't work (=> null ). The spec states: The getRealPath method takes a String argument and returns a String representation of a file on the local file system to which a path corresponds. Resources inside the META-INF/resources