Eclipse上的应用程序工作,以jar作为问题

问题描述 投票:-1回答:1

我的小应用程序在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$1.run(SshChooser.java:49)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.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)

我的结构:

enter image description here

我的推荐:

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;
    }
java properties jsch
1个回答
1
投票

始终使用getResourceAsStream()而不是getResource()导入文件

objFileInputStream = getClass().getClassLoader().getResourceAsStream(propFile)

你可以找到here的解释

© www.soinside.com 2019 - 2024. All rights reserved.