0

I've got an issue with my .jar file. It runs fine in Eclipse but as soon as I export it, it won't open. I've checked the manifest file and it looks like it's okay. Im using the ssh classes from here. Can anyone check the code and tell me, what's wrong, please?

Here's my code: Class 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:

<?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>
FranzHuber23
  • 1,779
  • 2
  • 12
  • 47
  • You probably should mention in which way it is not working. Command line arguments, class path, errors, stacktraces... – hotzst Nov 08 '15 at 09:15
  • I think there's a problem with file `Config.xml`. Try to debug the program in order to find out whether the file is loaded properly. I've experienced a lot of trouble with file loading from `.jar`. – petrbel Nov 08 '15 at 09:19
  • @petrbel: You're right. It says that the Config.xml is not found... – FranzHuber23 Nov 08 '15 at 09:22
  • @FranzHuber23 please see my answer – petrbel Nov 08 '15 at 09:27

2 Answers2

1

The problem is with loading the XML file from the .jar. 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. 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 (i.e. a single file, not a directory). Java implicitly can't address some "subfiles" of this file.

Community
  • 1
  • 1
petrbel
  • 2,442
  • 4
  • 24
  • 41
0

Here's the solution for it:

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

And the method 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;
    }
FranzHuber23
  • 1,779
  • 2
  • 12
  • 47