getresource

关于Java对相对目录的文件读写和调用方法的一些总结

Deadly 提交于 2019-12-02 04:47:46
关于 Java 对相对目录的文件读写和调用方法的一些总结 首先要讲 Java 路径 Java 中使用的路径,分为两种:绝对路径和相对路径。具体而言,又分为四种: 1. 本地系统的绝对路径: 如: windows: C:\abc\ projectName\bin\def. properties Linux: /us/home/name/projectName/bin/def.properties Java.io 包中的类,需要使用这种形式的参数。但是,它们一般也提供了 URI 类型的参数,而 URI 类型的参数,接受的是 URI 样式的 String 。因此,通过 URI 转换,还是可以把 URI 样式的绝对路径用在 java.io 包中的类中。 2.URI 形式的绝对资源路径 如: file:/C:/abc/def.properties URL 是 URI 的特例。 URL 的前缀 / 协议,必须是 Java 熟悉的。 URL 可以打开资源,而 URI 则不行。 URL 和 URI 对象可以互相转换,使用各自的 toURI(),toURL() 方法即可! 3. 相对于 classpath 的相对路径 如:相对于 file:/C:/abc/projectName/bin/def. properties/ 这个路径的相对路径。其中, bin 是本项目的 classpath 。所有的

log4j properties file bundled into jar in spark app is ignored

丶灬走出姿态 提交于 2019-12-01 11:19:53
问题 I need to read a custom log4j.properties from src/resources and this is not working try{ val inStream :InputStream= className.this.getClass.getClassLoader.getResourceAsStream("log4j.properties"); logCfgProps.load(inStream) } catch { case e: Throwable=> e.printStackTrace() log.error("log.properties file not present") } PropertyConfigurator.configure(logCfgProps) meaning that the log4j bundled in the jar is ignored. I cannot touch the log4j properties in the conf directory in the spark home.

Implementation of getResources() Android

家住魔仙堡 提交于 2019-12-01 09:39:19
I have just started learning android programming and I came up with a doubt about the method getResources(). I noticed that when I create a Resources object all I have to do is: Resources res = getResources(); The first doubt is the following why do I have to do in that way and I mustn't use the java keyword new ? Shouldn't I do something like this: Resources res = new Resources(); The second doubt is the following: at the top of my file I have imported the Resources class. import android.content.res.Resources; Now I read the android api and it says that getResources() is a public abstract

Implementation of getResources() Android

心已入冬 提交于 2019-12-01 09:07:02
问题 I have just started learning android programming and I came up with a doubt about the method getResources(). I noticed that when I create a Resources object all I have to do is: Resources res = getResources(); The first doubt is the following why do I have to do in that way and I mustn't use the java keyword new ? Shouldn't I do something like this: Resources res = new Resources(); The second doubt is the following: at the top of my file I have imported the Resources class. import android

How should I use getResource() in Java?

点点圈 提交于 2019-11-30 18:15:48
问题 This question is asked in numerous places, with myriad small variations. (Such as Java - getClassLoader().getResource() driving me bonkers among others.) I still can't make it work. Here's a code snippet: String clipName = "Chook.wav"; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // URL url = classLoader.getResource(clipName); URL url = new URL("file:///Users/chap/Documents/workspace/the1620/bin/ibm1620/" + clipName); ais = AudioSystem.getAudioInputStream(url);

How to get getclass().getResource() from a static context?

丶灬走出姿态 提交于 2019-11-30 07:53:26
I have a function where I am trying to load a file to a URL object, because the example project said so. public class SecureFTP { public static void main(String[] args) throws IOException , ClassNotFoundException, SQLException , JSchException, SftpException{ File file = new File("/home/xxxxx/.ssh/authorized_keys"); URL keyFileURL = this.getClass().getClassLoader().getResource(file); I tried using SecureFTP.class.getResource , but it still could not compile it. I am fairly new to Java, so I know I am doing something wrong. It can't compile because getResource takes a resource name (a String ,

Why does getResource return null

心不动则不痛 提交于 2019-11-30 05:47:20
问题 I'm trying to access a file in my project. But getResource method returns null. This is how my project looks like: Thread.currentThread().getContextClassLoader().getResource("assets/xxx.png"); //returns null And how project folder in eclipse workspace looks like: Why? I want to access files in my assets folder? Edit I created a jar file and this is content of the jar: Solved First of all, I've a lot of image files so I want to organize all them in a folder. I put the assets folder in src

Java Swing: unable to load image using getResource

蓝咒 提交于 2019-11-29 16:22:38
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("Frame1"); frame1.setSize(500, 500); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout

WildFly - getting resource from WAR

喜欢而已 提交于 2019-11-29 14:46:55
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/timers/ So, no wonder it doesn't work. It probably has something to do with JBoss module loading, but

Junit + getResourceAsStream Returning Null

一世执手 提交于 2019-11-28 22:31:01
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 where the file is located and the code itself). What small difference could there be preventing I assume