Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load file within a jar

Tags:

I need to package a configuration file within a jar. the configuration file is under the root of the jar file. However I got the following error:

Caused by: java.lang.IllegalArgumentException: URI is not hierarchical at java.io.File.(Unknown Source)

File url = new File(MyClass.class.getClassLoader().getResource("my.conf").toURI());
like image 223
user217631 Avatar asked Dec 28 '10 19:12

user217631


People also ask

Can JAR file be deployed?

You can make your simple archive or J2EE Client Module into an executable JAR file that you can launch with the java command. To deploy an executable JAR file: Select and right-click the simple archive or client icon in the Navigator to display the context menu.

How do I package a project into a JAR file?

2) Select Artifacts from the left-hand menu and then press the + icon. Select JAR and then From modules with dependencies. You don't need to change anything for the Module, however you do need to say which class in your project has the main method. Click the browse button to navigate to your main method.

How do I run a path from a JAR file?

In Java, we can use the following code snippets to get the path of a running JAR file. // static String jarPath = ClassName. class . getProtectionDomain() .


1 Answers

You should use getResourceAsStream() instead. If the file is embedded in your JAR the URI is most likely bundle:// URI

InputStream is = this.getClass().getResourceAsStream("my.conf");
like image 147
Andrew T Finnell Avatar answered Sep 19 '22 15:09

Andrew T Finnell