Discuss / Java / 直接运行下载好的源码,报无法加载主类,有大佬知道是啥原因吗?

直接运行下载好的源码,报无法加载主类,有大佬知道是啥原因吗?

Topic source

报错如下:

错误: 无法初始化主类 com.itranswarp.learnjava.AppConfig

原因: java.lang.NoClassDefFoundError: org/apache/catalina/WebResourceRoot

何小狗____

#2 Created at ... [Delete] [Delete and Lock User]

这是由maven中scope属性引起的,下面是Maven官网上的文档资料,以供参考。

Dependency scope is used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath.

There are 6 scopes:

  • compile
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided
    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. A dependency with this scope is added to the classpath used for compilation and test, but not the runtime classpath. It is not transitive.
  • runtime
    This scope indicates that the dependency is not required for compilation, but is for execution. Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath.
  • test
    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive. Typically this scope is used for test libraries such as JUnit and Mockito. It is also used for non-test libraries such as Apache Commons IO if those libraries are used in unit tests (src/test/java) but not in the model code (src/main/java).
  • system
    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import
    This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency is to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

bevarvv

#3 Created at ... [Delete] [Delete and Lock User]

请问怎么解决啊?我的也是报了同样的错误

栗栗志伟

#4 Created at ... [Delete] [Delete and Lock User]

注意:引入的Tomcat的scope为provided,在Idea下运行时,需要设置Run/Debug Configurations,选择Application - Main,钩上Include dependencies with "Provided" scope,这样才能让Idea在运行时把Tomcat相关依赖包自动添加到classpath中。
前边有说过这个注意点的。


  • 1

Reply