我在将文件从 VM 发送到另一台机器时遇到问题。就我而言,我可以在执行 java 文件时通过 SFTP 传输文件。
But when I am creating a .jar (executable) file and then trying to run that .jar (executable) file by using "java -jar filename.jar"
then I am getting a exception.
平均 java 文件工作正常,文件也成功传输,但是当我创建 .jar 文件并通过 .jar 文件时出现异常
**Java Source Code**
package com.arcanainfo.whatsapp.reportSFTP;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class WhatsappReport {
public static void main(String[] args) throws IOException {
System.out.println("flow start...");
FTPClient client = new FTPClient();
FileInputStream fis = null;
boolean result;
try {
client.connect("IP");
result = client.login("UserName", "Password");
if (result == true) {
System.out.println("Successfully logged in!");
} else {
System.out.println("Login Fail!");
return;
}
File file = new File("C:\\eclipseWorkSpace\\test123.txt");
String testName = file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);
if (result == true) {
System.out.println("File is uploaded successfully");
} else {
System.out.println("File uploading failed");
}
client.logout();
client.disconnect();
} catch (FTPConnectionClosedException e) {
System.out.println("FTPConnectionClosedException : ");
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (FTPConnectionClosedException e) {
System.out.println("FTPConnectionClosedException : "+e);
}
}
}
}
**POM.xml**
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>whatsapp_report_SFTP</groupId>
<artifactId>whatsapp_report_SFTP</artifactId>
<version>0.0.1</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- creating runable jar file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.arcanainfo.whatsapp.reportSFTP.WhatsappReport</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
**ERROR**
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPConnectionClosedException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.net.ftp.FTPConnectionClosedException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
提前致谢