Add properties file to build path of runnable jar

后端 未结 7 1639
無奈伤痛
無奈伤痛 2021-01-07 02:48

is it possible to add to the classpath of a runnable jar file some properties file? I tryed these solutions solutions:

  1. running the executable file using the

相关标签:
7条回答
  • 2021-01-07 02:52

    You can simply put the properties file in the META-INF folder.

    +META-INF
     +properties_dir
      +Test.properties
    
    0 讨论(0)
  • 2021-01-07 02:55

    You have to reference the .properties file in your class file relative to the .jar file location. The classpath seperators are different for Windows (semicolon) and Unix environments (colon).

    For windows

    java -classpath .;prop_dir; -jar Runnable.jar (In your Class file, the properties file should be accessed by relative URl from classpath say "prop_dir/prop1.properties")

    For Unix env

    java -classpath .:prop_dir: -jar Runnable.jar (In your Class file, the properties file should be accessed by relative URl from classpath say "prop_dir/prop1.properties")

    Code:

    enter image description here

    Output:

    enter image description here

    0 讨论(0)
  • 2021-01-07 03:04

    should it be like this prop_dir/prop1.properties prop_dir/prop1.properties in manifest?

    Also checkout this question: Java -jar : access external configuration file

    0 讨论(0)
  • 2021-01-07 03:09

    Adding a . to Class-Path of MANIFEST.MF and placing the properties file in the same level as the runnable jar works for me.

    java -jar MyExec.jar
    
    MANIFEST.MF
    Manifest-Version: 1.0
    Main-Class: com.test.MyMain
    Class-Path: . lib/MyDependent1.jar lib/MyDependent2.jar
    

    Code snippet to load properties file should be like

    ResourceBundle properties = ResourceBundle.getBundle("MyExec");
    
    0 讨论(0)
  • 2021-01-07 03:11

    I have a better approach to solve this

    My project structure is :- You can see there is no property file in this project structure

    package com.main;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class Run {
    
        public static void main(String[] args) throws IOException {
            Properties prop = new Properties();
            ClassLoader loader = Thread.currentThread().getContextClassLoader();           
            InputStream stream = loader.getResourceAsStream("config.properties");
            prop.load(stream);
            System.out.println(prop.getProperty("name"));
    
        }
    
    }
    

    Now place the property file within the same folder where you have exported the runnable jar file.

    For this approach you don't need to create a property file in eclipse you just have to create a property file where you export the runnable jar and remember don't give the path of a property file, write only the property file name like this

    InputStream stream = loader.getResourceAsStream("config.properties");

    0 讨论(0)
  • 2021-01-07 03:14

    In order to solve this problem i used a workaround: instead of running the runnable jar i runned the main class and I added the jar to the classpath.

    The command I used is the following one:

    java -classpath .;MyRunnableJar.jar;prop_dir; my.package.MyClass
    
    0 讨论(0)
提交回复
热议问题