我的.jar文件出現問題。它在Eclipse中運行良好,但只要我導出它就不會打開。我已經檢查過清單文件,看起來沒問題。 我使用從here ssh類。 任何人都可以檢查代碼並告訴我,有什麼不對嗎?Java程序在Eclipse中運行良好,但不是.jar文件(配置路徑是否錯誤?)
這裏是我的代碼: 類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來自-文件:
<?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>
你或許應該提到以何種方式它不工作。命令行參數,類路徑,錯誤,堆棧跟蹤... – hotzst
我認爲文件'Config.xml'有問題。嘗試調試該程序,以確定該文件是否已正確加載。我從'.jar'加載文件時遇到了很多麻煩。 – petrbel
@petrbel:你說得對。它說沒有找到Config.xml ... – FranzHuber23