getresource

Reading File In JAR using Relative Path

旧街凉风 提交于 2019-11-28 20:56:22
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 exists on a shared, mapped network drive Z:. When I run the application from command line I get this error:

Trying to load image using ImageIO.read(class.getResource(URL)) but getResource is returning null

寵の児 提交于 2019-11-28 13:55:24
I've been making a 2D game with my buddy and I've been learning a lot about some basic game dev concepts through some Youtube tutorials. One of the things I was learning about is sprites (for those that don't know, 2D images to render to the screen) and how to use them in my game. I've been using ImageIO.read(this.class.getResource(pathToMySprite)) but it seems that getResource() is returning null for some reason. I've been screwing around with the path a little, adding "/" in front of it, removing "/", putting the user.dir property to see if it needed the whole path, and I'm still getting the

WildFly - getting resource from WAR

天涯浪子 提交于 2019-11-28 08:42:30
问题 I am using the following method to get a resource from WAR file in WildFly: this.getClass().getResource(relativePath) It works when the application is deployed as exploded WAR. It used to work with compressed WAR, too. Yesterday, I did a clean and rebuild of project in Eclipse, and it just stopped working. When I check the resource root: logger.info(this.getClass().getResource("/").toExternalForm()); I get this: file:/C:/JBoss/wildfly8.1.0.CR1/modules/system/layers/base/org/jboss/as/ejb3/main

Why does class.getResource() keep returning null although there is a resource at the specified path?

眉间皱痕 提交于 2019-11-28 08:11:02
问题 I am wondering why the method getResource keeps returning null , I have the following setup: public static URL getResource(String path){ URL url = ResourceLoader.class.getResource(path); if (Parameters.DEBUG){ System.out.println(path); } return url; } My project structure in Eclipse is as follows: -- res -- img The path variable I pass to getResource has the value "/res/img" or "/res/img/smile.png" . Yet the method keeps returning null and url is not set. I also followed the instructions of

Access a resource outside a jar from the jar

ぐ巨炮叔叔 提交于 2019-11-28 01:01:43
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()); InputStream inputStream = this.getClass().getResourceAsStream(f.getParent() + "test.jpg"); Can you give

Java 加载资源文件

自闭症网瘾萝莉.ら 提交于 2019-11-27 11:15:06
简介 Java中获取资源的最常用的2中方式就是使用Class的getResource和使用ClassLoader的getResource方法,当然还有它们相关的方法。这里就介绍一下使用这2中方式的区别,和它们搜索使用的路径。 这里先说结论(hotspot): ClassLoader的getResource(name)方法会依次查找: 在"sun.boot.class.path"指定的路径问根目录下查找name资源 在"java.ext.dirs"指定的路径为根目录下查找name资源 在"java.class.path"指定的路径为根目录下查找name资源 利用ClassLoader(自定义的,重写了findResource)的findResource(name)获取URL Class的getResource(name)方法是调用的ClassLoader的getResource(name)方法,但是它做了2点处理: 如果name以"/"开头,就把name中开头的"/"去掉,然后调优ClassLoader的getResource(name)方法。然后在ClassLoader的getResource(name)方法搜索方式搜索。 如果name不以"/"开头,那么就用Class的包名+name作为新的name来调用ClassLoader的getResource(name)方法。cn

Trying to load image using ImageIO.read(class.getResource(URL)) but getResource is returning null

自作多情 提交于 2019-11-27 08:02:06
问题 I've been making a 2D game with my buddy and I've been learning a lot about some basic game dev concepts through some Youtube tutorials. One of the things I was learning about is sprites (for those that don't know, 2D images to render to the screen) and how to use them in my game. I've been using ImageIO.read(this.class.getResource(pathToMySprite)) but it seems that getResource() is returning null for some reason. I've been screwing around with the path a little, adding "/" in front of it,

Copy directory from a jar file

梦想与她 提交于 2019-11-27 01:48:39
I have recently developed an application and created the jar file. One of my classes creates an output directory, populating it with files from its resource. My code is something like this: // Copy files from dir "template" in this class resource to output. private void createOutput(File output) throws IOException { File template = new File(FileHelper.URL2Path(getClass().getResource("template"))); FileHelper.copyDirectory(template, output); } Unfortunately this doesn't work. I tried the following without luck: Using Streams to solve similar stuff on other classes but it doesn't work with dirs.

How to use ClassLoader.getResources() correctly? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-26 18:38:10
This question already has an answer here: How to list the files inside a JAR file? 13 answers How can I use ClassLoader.getResources() to find recursivly resources from my classpath? E.g. finding all resources in the META-INF "directory": Imagine something like getClass().getClassLoader().getResources("META-INF") Unfortunately, this does only retrieve an URL to exactly this "directory". all resources named bla.xml (recursivly) getClass().getClassLoader().getResources("bla.xml") But this returns an empty Enumeration . And as a bonus question: How does ClassLoader.getResources() differ from

Class.getResource() returns null

北慕城南 提交于 2019-11-26 14:43:28
I am trying to display pictures on the JPanel but I keep getting the error : java.lang.IllegalArgumentException: input == null! I don't understand what is happening. Here is the code I am using: public void actionPerformed(ActionEvent e) { try { Image image=ImageIO.read(getClass().getResource("img/" +num.getText()+".jpg")); Image resized = image.getScaledInstance(200, 200, 100); pictureFrame.setIcon(new ImageIcon(resized)); } catch (Exception ex){ ex.printStackTrace(); } } This just leads to me getting the error! Stack trace produces the following: Java.lang.IllegalArgumentException: input ==