Windows迁移Ubuntu后Java程序无法运行问题求助
Java程序从Windows迁移到Ubuntu后无法找到主类的解决方法
问题场景
将原本在Windows系统运行的Java程序迁移到Ubuntu后,启动时报错无法找到主类com.playmore.http.jetty.SpringContextLoader。
Windows原启动脚本(bat)
@echo off color 1f title UC C:\*****\Java\jdk1.8.0_121\bin\java -Xmx512m -Xms256m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log -classpath "./;./lib/*;./config" com.playmore.http.jetty.SpringContextLoader pause
转换后的Ubuntu启动脚本(sh)
#!/bin/bash echo -e "\e[34m" echo -e "\e[1mUC\e[0m" java -Xmx512m -Xms256m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log -classpath "./:./lib/*:./config" com.playmore.http.jetty.SpringContextLoader read -p "Press Enter to continue..."
报错信息
Error: Could not find or load main class com.playmore.http.jetty.SpringContextLoader Caused by: java.lang.ClassNotFoundException: com.playmore.http.jetty.SpringContextLoader Press Enter to continue...
排查与解决步骤
1. 修正Classpath的通配符处理
Windows与Linux的classpath分隔符已替换为:,但Linux下shell会自动展开./lib/*为具体文件名,导致Java无法识别为加载所有jar包的指令。需将classpath整体用双引号包裹,让Java自行解析通配符:
java -Xmx512m -Xms256m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log -classpath "./:./lib/*:./config" com.playmore.http.jetty.SpringContextLoader
2. 匹配Java版本
Windows使用的是JDK 1.8.0_121,在Ubuntu终端执行java -version检查当前Java版本,确保为JDK 1.8系列,版本差异可能导致类加载兼容性问题。
3. 验证目录结构与权限
- 确认Ubuntu上的项目目录结构与Windows完全一致:当前目录下存在编译后的class文件(或包含主类的jar包)、
lib目录下有所有依赖jar、config目录存在。 - 检查文件权限:执行
ls -l查看class文件、jar包、config文件的读取权限,若权限不足,用chmod +r <文件/目录>添加读取权限。 - 手动确认主类存在:如果是直接运行class文件,检查
./com/playmore/http/jetty/SpringContextLoader.class是否存在;如果主类在jar包中,用jar tf <jar文件名>查看jar包内是否包含该类。
4. 调试类加载过程
添加-verbose:class参数启动程序,查看类加载的详细日志,确认Java是否在正确的路径中查找主类:
java -Xmx512m -Xms256m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log -classpath "./:./lib/*:./config" -verbose:class com.playmore.http.jetty.SpringContextLoader
5. 确保日志目录存在
脚本中指定了-Xloggc:logs/gc.log,若logs目录不存在,Java可能抛出异常影响启动,提前创建目录:
mkdir -p logs
内容的提问来源于stack exchange,提问作者errorsnyc
相关产品推荐
相关产品推荐

