我的.jar文件存在问题。它在Eclipse中运行良好,但是一旦我导出它,它就不会打开。我已经检查了清单文件,看起来没问题。 我正在使用here中的ssh类。 任何人都可以查看代码并告诉我,有什么问题吗?
这是我的代码: 类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;
}
}
班级配置:
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;
}
}
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>
答案 0 :(得分:1)
问题在于从.jar
加载XML文件。您可以改用以下命令:
InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");
但请注意,input
变量不是File
,也不是String
,而是InputStream。但是,您可以将流的整个内容读入字符串并稍后解析(请参阅this answer)
为了理解这个问题,我必须意识到.jar
只是一个存档(即一个文件,而不是一个目录)。 Java隐含地无法解决一些&#34;子文件&#34;这个文件。
答案 1 :(得分:0)
以下是解决方案:
InputStream input = SSHServerRestart.class.getResourceAsStream("/SSHServerRestartPackage/Config.xml");
config = InitConfiguration(input);
方法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;
}