Could not find the main class.Program will exit.
我新建maven項目 寫了一個測試類 運行時報錯
import java.sql.*;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* ClassName:SpringTest <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2018年5月23日 上午10:43:40 <br/>
* @author Administrator
* @version
* @since JDK 1.6
* @see
*/
public class SpringTest {
public static void main(String[] args) {
try{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource", DataSource.class);
String sql = "select * from user";
Connection connection = dataSource.getConnection();
Statement stm = connection.createStatement();
ResultSet rs = stm.executeQuery(sql);
while (rs.next())
{System.out.println("用戶名為:");
System.out.println(rs.getString(2));
}
}catch(Exception e){
}
}}
網上查詢了一些資料 說大概有三種情況
一、Java類中沒有寫Main方法:
針對著這種問題直接在Java類中增加Main方法即可;
二、項目文件的編譯環境的JDK版本高於運行環境的JDK版本:
解決方法:
在項目上右擊--->Properties--->Java Compiler,
將運行環境改成與編譯環境一致
三、項目中引用的Jar包的編譯環境JDK版本高於當前運行環境的JDK版本:
顯然一不是我的情況 然後看jdk版本,發現確實比我本地的版本高
改了一下可以運行了,但是發現項目報錯了
Java compiler level does not match the version of the installed Java project facet. maven-webapp Unknown Faceted Project Problem (Java Version Mismatch)
郁悶了,又問了一下度娘 說是 maven update一下就可以了
試了一下果然可以了,但是又出現了原先的問題。這難道是死循環嗎?太坑了吧!
最後仔細想了想是maven的時候把項目又改為1.7了
修改一下
最後maven一下ok了
Could not find the main class.Program will exit.