简体   繁体   English

Java程序可以在Eclipse中正常运行,但不能作为.jar文件运行(配置路径错误吗?)

[英]Java program runs fine in Eclipse but not as a .jar file (Is the config path wrong??)

I've got an issue with my .jar file. 我的.jar文件有问题。 It runs fine in Eclipse but as soon as I export it, it won't open. 它在Eclipse中运行良好,但是一旦导出,它就不会打开。 I've checked the manifest file and it looks like it's okay. 我检查了清单文件,看起来还可以。 Im using the ssh classes from here . 我从这里开始使用ssh类。 Can anyone check the code and tell me, what's wrong, please? 谁能检查代码并告诉我,怎么了?

Here's my code: Class SSHServerRestart: 这是我的代码:SSHServerRestart类:

package SSHServerRestartPackage;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

import java.beans.XMLDecoder;
import java.io.*;
import java.net.URL;
import java.util.Properties;

public class SSHServerRestart
{
    public static void main(String args[]) throws IOException
    {   
        Config config = new Config();

        try
        {
            URL location = SSHServerRestart.class.getProtectionDomain().getCodeSource().getLocation();
            config = InitConfiguration(location.getPath() + "Config.xml");
            System.out.println("Konfiguration geladen.");

            JSch jsch = new JSch();
            Session session = jsch.getSession(config.getUser(), config.getServerName(), config.getServerPort());
            session.setPassword(config.getPassword());
            Properties props = new Properties();
            props.put("StrictHostKeyChecking", "no");
            session.setConfig(props);
            session.connect();
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            System.out.println("Server wird neu gestartet");
            channel.setCommand("reboot");
            channel.connect();
            channel.disconnect();
            session.disconnect();
            System.out.println("Beliebige Taste drücken, um das Programm zu beenden");
            System.in.read();
        }
        catch (Exception e) 
        {
            System.err.println(e.getMessage());
            System.out.println("Beliebige Taste drücken, um das Programm zu beenden");
            System.in.read();
        }
    }

    private static Config InitConfiguration(String filename) throws IOException
    {
        Config configTemp = new Config();
        try
        {
            FileInputStream fis = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            XMLDecoder xmlDecoder = new XMLDecoder(bis);
            configTemp = (Config) xmlDecoder.readObject();
            xmlDecoder.close();
        }
        catch (Exception e) 
        {
            System.err.println(e.getMessage());
            System.out.println("Beliebige Taste drücken, um das Programm zu beenden");
            System.in.read();
        }
        return configTemp;
    }
}

Class Config: 类配置:

package SSHServerRestartPackage;

import java.io.Serializable;

public class Config implements Serializable
{
    private static final long serialVersionUID = 1L;
    private String serverName;
    private int serverPort;
    private String user;
    private String password;

    public String getServerName() 
    {
        return serverName;
    }
    public void setServerName(String serverName) 
    {
        this.serverName = serverName;
    }
    public int getServerPort() 
    {
        return serverPort;
    }
    public void setServerPort(int serverPort) 
    {
        this.serverPort = serverPort;
    }
    public String getUser() 
    {
        return user;
    }
    public void setUser(String user) 
    {
        this.user = user;
    }
    public String getPassword() 
    {
        return password;
    }
    public void setPassword(String password) 
    {
        this.password = password;
    }
}

And the Config.xml-File: 和Config.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<java version="1.8.0_20" class="SSHServerRestartPackage.SSHServerRestart">
    <object class="SSHServerRestartPackage.Config">
        <void property="serverName">
            <string>202.202.202.202</string>
        </void>
        <void property="serverPort">
            <int>22</int>
        </void>
        <void property="user">
            <string>root</string>
        </void>
        <void property="password">
            <string>IhrPasswort</string>
        </void>
    </object>
</java>

The problem is with loading the XML file from the .jar . 问题是从.jar加载XML文件。 You may use the following command instead: 您可以改用以下命令:

InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");

But be aware, the input variable isn't a File nor String but InputStream . 但是请注意, input变量不是File也不是String而是InputStream However, you may read the whole content of the stream into string and parse it later (see this answer ) 但是,您可以将流的全部内容读入字符串并稍后解析(请参阅此答案

In order to understand the problem, one must realize that .jar is simply an archive (ie a single file, not a directory). 为了理解该问题,必须意识到.jar只是一个存档(即单个文件,而不是目录)。 Java implicitly can't address some "subfiles" of this file. Java隐式无法处理此文件的某些“子文件”。

Here's the solution for it: 这是解决方案:

InputStream input = SSHServerRestart.class.getResourceAsStream("/SSHServerRestartPackage/Config.xml");
        config = InitConfiguration(input);

And the method InitConfiguration: 以及方法InitConfiguration:

private static Config InitConfiguration(InputStream bis) throws IOException
    {
        Config configTemp = new Config();
        try
        {
            XMLDecoder xmlDecoder = new XMLDecoder(bis);
            configTemp = (Config) xmlDecoder.readObject();
            xmlDecoder.close();
        }
        catch (Exception e) 
        {
            System.err.println(e.getMessage());
            System.out.println("Beliebige Taste drücken, um das Programm zu beenden");
            System.in.read();
        }
        return configTemp;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java程序可以在Eclipse中正常运行,但不能作为.jar文件运行 - Java program runs fine in Eclipse but not as a .jar file 我的程序在Eclipse中运行良好,但抛出作为jar文件运行的异常 - My program runs fine in Eclipse but throws exception running as a jar file Hadoop MapReduce程序在Eclipse中运行良好,但在导出到.jar文件时却无法正常运行 - Hadoop MapReduce program runs fine in Eclipse but not when exported to .jar file 程序在Eclipse中可以正常运行,但是在导出到.jar文件时却不能正常运行 - program runs fine in Eclipse but not when exported to .jar file 程序可以在IDE中正常运行,但不能作为Jar文件运行 - Program runs fine in IDE but not as Jar file Java程序在Eclipse中运行,但在jar中出现错误 - Java program runs in eclipse but errors in jar Eclipse代码运行良好,Jar不能 - Eclipse code runs fine, jar not Java项目在Eclipse中运行,但在jar文件中不正确 - java project runs in eclipse but not correct in jar file 我的Java程序在netbeans中运行,但不会在命令中或通过jar文件运行。 我究竟做错了什么? - My java program runs in netbeans, but will not run in command or through the jar file. What am I doing wrong? Java程序在Netbeans中运行顺利,但在Eclipse中慢慢运行,并作为可执行jar - Java program runs smoothly in Netbeans but slowly in Eclipse and as an executable jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM