应用程序在 Eclipse 上工作,将问题作为 jar

Application work on Eclipse, give problem as jar

我的小应用程序在 Eclipse 上运行完美,但当我尝试从命令行调用它的 jar 时出现问题。问题看起来像是在保护我的资源文件夹,我在其中放置了一些属性文件。

java.io.FileNotFoundException: file:*****************************\EasyDeployment\target\EasyDeployment-0.0.1-SNAPSHOT.jar!\ssh.properties
 (the syntax of the file name and the directory name or the value label it is not correct) // I translate this part that was in my native language
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at Configuration.PropertiesMenager.getAllKeys(PropertiesMenager.java:91)
        at GraphicInterface.SshChooser.initialize(SshChooser.java:101)
        at GraphicInterface.SshChooser.<init>(SshChooser.java:62)
        at GraphicInterface.SshChooser.run(SshChooser.java:49)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access0(Unknown Source)
        at java.awt.EventQueue.run(Unknown Source)
        at java.awt.EventQueue.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

我的结构:

我的参考资料:

public final static String SSH_PROPERTIES = "ssh.properties";
    public final static String PORT_PROPERTIES = "port.properties" ;
    public final static String CMD_PROPERTIES = "cmd.properties" ;
    public final static String CONF_PROPERTIES = "configuration.properties";

调用这些属性的方法示例:

public String getPropertiyByKey(String key, String propFile) throws IOException {
        Properties prop = new Properties();
        FileInputStream objFileInputStream = null;
        objClassLoader = getClass().getClassLoader();
        String result = "";
        try {
            System.out.println(propFile);
            objFileInputStream = new FileInputStream(objClassLoader.getResource(propFile).getFile());
            prop.load(objFileInputStream);
            result=prop.getProperty(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            objFileInputStream.close();
        }   
        return result;
    }

始终使用 getResourceAsStream() 而不是 getResource()

导入文件
objFileInputStream = getClass().getClassLoader().getResourceAsStream(propFile)

你可以找到解释here