eclipse springboot运行helloworld错误: 找不到或无法加载主类 xxx.xxx.xxx
这个错误,在网上搜找了好久,说是什么jar包冲突,什么环境配置,我经过验证均是正确的,javac java java -version 都没问题,环境变量也OK,各种解释均没有能够解决我的问题,最后好一顿折腾,解决了
shiro 的测试方法
1 package com.sharp.forward; 2 3 import org.apache.shiro.SecurityUtils; 4 import org.apache.shiro.authc.UsernamePasswordToken; 5 import org.apache.shiro.mgt.DefaultSecurityManager; 6 import org.apache.shiro.realm.Realm; 7 import org.apache.shiro.realm.SimpleAccountRealm; 8 import org.apache.shiro.subject.Subject; 9 import org.junit.Before; 10 import org.junit.Test; 11 12 public class JavaShiroTest extends ShiroApplicationTests{ 13 14 SimpleAccountRealm realm = new SimpleAccountRealm("simpleRealm"); 15 @Before 16 public void add() { 17 realm.addAccount("张三", "123456"); 18 } 19 20 @Test 21 public void contextLoads() { 22 } 23 @Test 24 public void testAuthenticate() { 25 // 三要素,SecurityManager,subject,realm 26 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); 27 // SecurityManage设置realm与数据库或缓存相连接 28 defaultSecurityManager.setRealm(realm); 29 // SecurityUtils设置SecurityManager可以与主题subject相连接 30 SecurityUtils.setSecurityManager(defaultSecurityManager); 31 // 获取主题,并通过secutityUtil将securityManager和realm联系起来, 32 Subject subject = SecurityUtils.getSubject(); 33 // 提交主题是通过token提交的 34 UsernamePasswordToken token = new UsernamePasswordToken("张三", "123456"); 35 subject.login(token); 36 System.out.println("subject.isAuthenticated(): "+subject.isAuthenticated()); 37 38 39 40 } 41 42 }
在测试类里面的测试方法报错,
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=testAuthenticate], {ExactMatcher:fDisplayName=testAuthenticate(com.sharp.forward.JavaShiroTest)], {LeadingIdentifierMatcher:fClassName=com.sharp.forward.JavaShiroTest,fLeadingIdentifier=testAuthenticate]] from org.junit.internal.requests.ClassRequest@73c6c3b2
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:80)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:71)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
然后在主类中写了一个main查看是否报错
然后找解决办法,
说明首先,我是创建的springboot工程,创建的时候,因为之前选择版本的问题,出来的项目总不是完整的项目,需要修改版本,可能是这个原因导致的一些配置发生了变化,现在先看一下我的创建过程,我的项目都是自己搭建的私服
当出现不完整的项目的时候,就把项目的版本修改为适应的版本就可以出来了,可能是由于记忆了,
之前出现题目中的问题是运行其他项目中突然出现的,于是我仍然在同样的环境下建的项目但运行helloworld
就是main方法打印helloworld,控制台同样出现了错误:找不到或无法加载主类的提示,
最终解决办法是:先run build install 此时发现不能成功,提示用的是jre不是jdk,然后修改为jdk,再次运行一切都好了,
然后我再去创建项目,除了将https改为http外,其他的完整项目均能一次加载,不需要修改版本了。
这样解决了,
但是我又重新复现了一下这个问题,发现单单修改了这里有时候还不行,maven install 仍然报jre而不是jdk,同样又出现上述问题,于是我又修改如下地方
项目右键属性,下方修改为jdk,
再次 install 然后再去运行helloworld和测试,完美解决,看来还是jdk在作怪。
测试运行结果
junit变为绿条了
控制台打印结果如下
1 16:41:35.128 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.sharp.forward.JavaShiroTest] 2 16:41:35.135 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 3 16:41:35.147 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 4 16:41:35.171 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.sharp.forward.JavaShiroTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 5 16:41:35.189 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.sharp.forward.JavaShiroTest], using SpringBootContextLoader 6 16:41:35.199 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.sharp.forward.JavaShiroTest]: class path resource [com/sharp/forward/JavaShiroTest-context.xml] does not exist 7 16:41:35.200 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.sharp.forward.JavaShiroTest]: class path resource [com/sharp/forward/JavaShiroTestContext.groovy] does not exist 8 16:41:35.200 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.sharp.forward.JavaShiroTest]: no resource found for suffixes {-context.xml, Context.groovy}. 9 16:41:35.201 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.sharp.forward.JavaShiroTest]: JavaShiroTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 10 16:41:35.284 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.sharp.forward.JavaShiroTest] 11 16:41:35.397 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\worksheet\shiro\target\classes\com\sharp\forward\ShiroApplication.class] 12 16:41:35.404 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.sharp.forward.ShiroApplication for test class com.sharp.forward.JavaShiroTest 13 16:41:35.538 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.sharp.forward.JavaShiroTest]: using defaults. 14 16:41:35.540 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] 15 16:41:35.592 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] 16 16:41:35.593 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] 17 16:41:35.593 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6e171cd7, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@402bba4f, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@795cd85e, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@59fd97a8, org.springframework.test.context.support.DirtiesContextTestExecutionListener@f5ac9e4, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@123ef382, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@dbf57b3, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@384ad17b, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@61862a7f, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@441772e] 18 16:41:35.596 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 19 16:41:35.596 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 20 16:41:35.598 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 21 16:41:35.598 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 22 16:41:35.672 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 23 16:41:35.672 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 24 16:41:35.676 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 25 16:41:35.676 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 26 16:41:35.677 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 27 16:41:35.682 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 28 16:41:35.690 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@9f116cc testClass = JavaShiroTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@12468a38 testClass = JavaShiroTest, locations = '{}', classes = '{class com.sharp.forward.ShiroApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@76b0bfab, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2f943d71, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3e57cd70, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5bcea91b], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null]. 29 16:41:35.691 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sharp.forward.JavaShiroTest] 30 16:41:35.691 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sharp.forward.JavaShiroTest] 31 16:41:35.734 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1} 32 33 . ____ _ __ _ _ 34 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 35 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 36 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 37 ' |____| .__|_| |_|_| |_\__, | / / / / 38 =========|_|==============|___/=/_/_/_/ 39 :: Spring Boot :: (v2.1.0.RELEASE) 40 41 2019-03-02 16:41:36.379 INFO 18776 --- [ main] com.sharp.forward.JavaShiroTest : Starting JavaShiroTest on litan with PID 18776 (started by litan in D:\worksheet\shiro) 42 2019-03-02 16:41:36.382 INFO 18776 --- [ main] com.sharp.forward.JavaShiroTest : No active profile set, falling back to default profiles: default 43 2019-03-02 16:41:39.311 INFO 18776 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 44 2019-03-02 16:41:39.904 INFO 18776 --- [ main] com.sharp.forward.JavaShiroTest : Started JavaShiroTest in 4.153 seconds (JVM running for 5.806) 45 2019-03-02 16:41:40.645 INFO 18776 --- [ main] a.s.s.m.AbstractValidatingSessionManager : Enabling session validation scheduler... 46 subject.isAuthenticated(): true 47 2019-03-02 16:41:41.163 INFO 18776 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
helloworld运行结果
参考https://www.cnblogs.com/fengan/p/5904445.html,重建project有时也可以解决