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());
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.
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.
In Java, we can use the following code snippets to get the path of a running JAR file. // static String jarPath = ClassName. class . getProtectionDomain() .
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With