为什么我的Maven构建在执行时找不到Netty的EventLoopGroup类?

huangapple go评论148阅读模式
英文:

Why is my maven build not finding Netty's EventLoopGroup class when executing?

问题

我根据你提供的内容为你翻译如下:

我按照Netty的文档编写了我的代码,然后执行了:

  1. mvn package

它成功构建了。然后我运行了:

  1. java -jar target/netty-listener-0.0.1-SNAPSHOT.jar

但是它给我返回了这个错误:

> 异常线程 "main" java.lang.NoClassDefFoundError:
> io/netty/channel/EventLoopGroup at
> java.lang.Class.getDeclaredMethods0(Native Method) at
> java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at
> java.lang.Class.privateGetMethodRecursive(Class.java:3048) at
> java.lang.Class.getMethod0(Class.java:3018) at
> java.lang.Class.getMethod(Class.java:1784) at
> sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
> at
> sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
> Caused by: java.lang.ClassNotFoundException:
> io.netty.channel.EventLoopGroup at
> java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
> java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
> sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at
> java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more

据我理解,JRE在执行时找不到Netty的EventLoopGroup类。在Eclipse中,我在Maven的依赖选项卡中看到了netty-all依赖项,并且EventLoopGroup也在那里。我尝试过多次更改Netty的版本,但都没有成功。

这是我的主要代码:

  1. package paplistener;
  2. import io.netty.bootstrap.ServerBootstrap;
  3. import io.netty.channel.ChannelFuture;
  4. import io.netty.channel.ChannelInitializer;
  5. import io.netty.channel.ChannelOption;
  6. import io.netty.channel.EventLoopGroup;
  7. import io.netty.channel.nio.NioEventLoopGroup;
  8. import io.netty.channel.socket.SocketChannel;
  9. import io.netty.channel.socket.nio.NioServerSocketChannel;
  10. public class PAPServer
  11. {
  12. private int port;
  13. public PAPServer(int port)
  14. {
  15. this.port = port;
  16. }
  17. public void run() throws Exception
  18. {
  19. EventLoopGroup bossGroup = new NioEventLoopGroup();
  20. EventLoopGroup workerGroup = new NioEventLoopGroup();
  21. try {
  22. ServerBootstrap b = new ServerBootstrap();
  23. b.group(bossGroup, workerGroup)
  24. .channel(NioServerSocketChannel.class)
  25. .childHandler(new ChannelInitializer<SocketChannel>() {
  26. @Override
  27. public void initChannel(SocketChannel ch) throws Exception {
  28. ch.pipeline().addLast(new CommandDecoder(),
  29. new PAPServerHandler());
  30. }
  31. })
  32. .option(ChannelOption.SO_BACKLOG, 128)
  33. .childOption(ChannelOption.SO_KEEPALIVE, true);
  34. ChannelFuture f = b.bind(port).sync();
  35. f.channel().closeFuture().sync();
  36. } finally {
  37. workerGroup.shutdownGracefully();
  38. bossGroup.shutdownGracefully();
  39. }
  40. }
  41. public static void main(String[] args) throws Exception
  42. {
  43. int port = 8080;
  44. if(args.length > 0)
  45. {
  46. port = Integer.parseInt(args[0]);
  47. }
  48. new PAPServer(port).run();
  49. }
  50. }

这几乎是从Netty文档中复制的代码,但它在管道开头添加了一个解码器。

这是我的pom.xml:

  1. <!-- 在这里是你的pom.xml内容 -->

我应该如何解决这个问题?

编辑1:向pom.xml添加了maven-shade-plugin,但(相同的)问题仍然存在。

添加到pom的行:

  1. <!-- 在这里是你向pom.xml添加的内容 -->

编辑2:向pom.xml添加了artifactSet,但没有起作用。

  1. <!-- 在这里是你向pom.xml添加的内容 -->

我还尝试了 * : *,就像Yuri G.建议的一样。

编辑3:unzip -l的内容:

  1. <!-- 在这里是unzip -l的内容 -->

看起来netty没有被复制到jar文件中。但我仍然不知道如何解决它。我会在这里链接生成的.classpath文件(我猜是Eclipse生成的):

  1. <!-- 在这里是.classpath的内容 -->

这些是我翻译好的部分,关于具体的问题和解决方案,请查阅原文。

英文:

I wrote my code following Netty's documentation, then I execute:

  1. mvn package

It builds successfully. Then I run:

  1. java -jar target/netty-listener-0.0.1-SNAPSHOT.jar

And it promts me with this error:

> Exception in thread "main" java.lang.NoClassDefFoundError:
> io/netty/channel/EventLoopGroup at
> java.lang.Class.getDeclaredMethods0(Native Method) at
> java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at
> java.lang.Class.privateGetMethodRecursive(Class.java:3048) at
> java.lang.Class.getMethod0(Class.java:3018) at
> java.lang.Class.getMethod(Class.java:1784) at
> sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
> at
> sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
> Caused by: java.lang.ClassNotFoundException:
> io.netty.channel.EventLoopGroup at
> java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
> java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
> sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at
> java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more

To my understanding, the JRE is not finding Netty's EventLoopGroup Class in execution time. In Eclipse, I see the netty-all dependency on Maven's dependency tab and EventLoopGroup is there. I've tried to change Netty's version multiple times but it does not work.

Here is my main code:

  1. package paplistener;
  2. import io.netty.bootstrap.ServerBootstrap;
  3. import io.netty.channel.ChannelFuture;
  4. import io.netty.channel.ChannelInitializer;
  5. import io.netty.channel.ChannelOption;
  6. import io.netty.channel.EventLoopGroup;
  7. import io.netty.channel.nio.NioEventLoopGroup;
  8. import io.netty.channel.socket.SocketChannel;
  9. import io.netty.channel.socket.nio.NioServerSocketChannel;
  10. public class PAPServer
  11. {
  12. private int port;
  13. public PAPServer(int port)
  14. {
  15. this.port = port;
  16. }
  17. public void run() throws Exception
  18. {
  19. EventLoopGroup bossGroup = new NioEventLoopGroup();
  20. EventLoopGroup workerGroup = new NioEventLoopGroup();
  21. try {
  22. ServerBootstrap b = new ServerBootstrap();
  23. b.group(bossGroup, workerGroup)
  24. .channel(NioServerSocketChannel.class)
  25. .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {
  26. @Override
  27. public void initChannel(SocketChannel ch) throws Exception {
  28. ch.pipeline().addLast(new CommandDecoder(),
  29. new PAPServerHandler());
  30. }
  31. })
  32. .option(ChannelOption.SO_BACKLOG, 128)
  33. .childOption(ChannelOption.SO_KEEPALIVE, true);
  34. ChannelFuture f = b.bind(port).sync();
  35. f.channel().closeFuture().sync();
  36. } finally {
  37. workerGroup.shutdownGracefully();
  38. bossGroup.shutdownGracefully();
  39. }
  40. }
  41. public static void main(String[] args) throws Exception
  42. {
  43. int port = 8080;
  44. if(args.length &gt; 0)
  45. {
  46. port = Integer.parseInt(args[0]);
  47. }
  48. new PAPServer(port).run();
  49. }
  50. }

It is almost the exact code from Netty's documentation, but it adds a decoder in the beginning of the pipeline.

Here is my pom.xml:

  1. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  2. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  3. &lt;groupId&gt;neurony.listener&lt;/groupId&gt;
  4. &lt;artifactId&gt;netty-listener&lt;/artifactId&gt;
  5. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  6. &lt;packaging&gt;jar&lt;/packaging&gt;
  7. &lt;dependencies&gt;
  8. &lt;dependency&gt;
  9. &lt;groupId&gt;io.netty&lt;/groupId&gt;
  10. &lt;artifactId&gt;netty-all&lt;/artifactId&gt;
  11. &lt;version&gt;4.1.53.Final&lt;/version&gt;
  12. &lt;scope&gt;compile&lt;/scope&gt;
  13. &lt;/dependency&gt;
  14. &lt;dependency&gt;
  15. &lt;groupId&gt;javassist&lt;/groupId&gt;
  16. &lt;artifactId&gt;javassist&lt;/artifactId&gt;
  17. &lt;version&gt;3.12.1.GA&lt;/version&gt;
  18. &lt;/dependency&gt;
  19. &lt;/dependencies&gt;
  20. &lt;build&gt;
  21. &lt;pluginManagement&gt;
  22. &lt;plugins&gt;
  23. &lt;plugin&gt;
  24. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  25. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  26. &lt;version&gt;3.1&lt;/version&gt;
  27. &lt;configuration&gt;
  28. &lt;source&gt;1.8&lt;/source&gt;
  29. &lt;target&gt;1.8&lt;/target&gt;
  30. &lt;/configuration&gt;
  31. &lt;/plugin&gt;
  32. &lt;plugin&gt;
  33. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  34. &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
  35. &lt;version&gt;2.4&lt;/version&gt;
  36. &lt;configuration&gt;
  37. &lt;archive&gt;
  38. &lt;manifest&gt;
  39. &lt;mainClass&gt;paplistener.PAPServer&lt;/mainClass&gt;
  40. &lt;/manifest&gt;
  41. &lt;/archive&gt;
  42. &lt;/configuration&gt;
  43. &lt;/plugin&gt;
  44. &lt;plugin&gt;
  45. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  46. &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
  47. &lt;version&gt;2.5.1&lt;/version&gt;
  48. &lt;executions&gt;
  49. &lt;execution&gt;
  50. &lt;id&gt;copy-dependencies&lt;/id&gt;
  51. &lt;phase&gt;package&lt;/phase&gt;
  52. &lt;goals&gt;
  53. &lt;goal&gt;copy-dependencies&lt;/goal&gt;
  54. &lt;/goals&gt;
  55. &lt;configuration&gt;
  56. &lt;outputDirectory&gt;
  57. ${project.build.directory}/dependency-jars/
  58. &lt;/outputDirectory&gt;
  59. &lt;/configuration&gt;
  60. &lt;/execution&gt;
  61. &lt;/executions&gt;
  62. &lt;/plugin&gt;
  63. &lt;/plugins&gt;
  64. &lt;/pluginManagement&gt;
  65. &lt;/build&gt;
  66. &lt;/project&gt;

What can I do to solve this problem?

EDIT1: Added maven-shade-plugin to pom.xml but the (same) problem persists.

Lines added to pom:

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  3. &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;3.2.4&lt;/version&gt;
  5. &lt;executions&gt;
  6. &lt;execution&gt;
  7. &lt;phase&gt;package&lt;/phase&gt;
  8. &lt;goals&gt;
  9. &lt;goal&gt;shade&lt;/goal&gt;
  10. &lt;/goals&gt;
  11. &lt;configuration&gt;
  12. &lt;transformers&gt;
  13. &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;
  14. &lt;manifestEntries&gt;
  15. &lt;Main-Class&gt;paplistener.PAPServer&lt;/Main-Class&gt;
  16. &lt;Build-Number&gt;1&lt;/Build-Number&gt;
  17. &lt;/manifestEntries&gt;
  18. &lt;/transformer&gt;
  19. &lt;/transformers&gt;
  20. &lt;/configuration&gt;
  21. &lt;/execution&gt;
  22. &lt;/executions&gt;
  23. &lt;/plugin&gt;

EDIT2: added artifactSet to pom.xml but it didn't work

  1. &lt;artifactSet&gt;
  2. &lt;includes&gt;
  3. &lt;include&gt;io.netty:*&lt;/include&gt;
  4. &lt;/includes&gt;
  5. &lt;/artifactSet&gt;

I tried * : * as well, just as Yuri G. suggested.

EDIT3: Contents of unzip -l:

> Archive: netty-listener-0.0.1-SNAPSHOT.jar Length Date Time
> Name
> --------- ---------- ----- ----
> 0 10-29-2020 10:17 META-INF/
> 266 10-29-2020 10:17 META-INF/MANIFEST.MF
> 0 10-29-2020 10:17 paplistener/
> 2903 10-29-2020 10:17 paplistener/PAPCommandHandler.class
> 2554 10-29-2020 10:17 paplistener/PAPServer.class
> 2103 10-29-2020 10:17 paplistener/CommandDecoder.class
> 2834 10-29-2020 10:17 paplistener/PAPServerHandler.class
> 1319 10-29-2020 10:17 paplistener/PAPServer$1.class
> 0 10-29-2020 10:17 META-INF/maven/
> 0 10-29-2020 10:17 META-INF/maven/neurony.listener/
> 0 10-29-2020 10:17 META-INF/maven/neurony.listener/netty-listener/
> 3572 10-28-2020 12:29 META-INF/maven/neurony.listener/netty-listener/pom.xml
> 125 10-29-2020 10:17 META-INF/maven/neurony.listener/netty-listener/pom.properties
> --------- -------
> 15676 13 files

It seems like netty is not being copied to the jar file. But I still don't know how to solve it. I'll link here the generated .classpath (I guess Eclipse generated it):

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;classpath&gt;
  3. &lt;classpathentry kind=&quot;src&quot; path=&quot;src/test/java&quot; output=&quot;target/test-classes&quot; including=&quot;**/*.java&quot;/&gt;
  4. &lt;classpathentry kind=&quot;src&quot; path=&quot;src/test/resources&quot; output=&quot;target/test-classes&quot; excluding=&quot;**/*.java&quot;/&gt;
  5. &lt;classpathentry kind=&quot;src&quot; path=&quot;src/main/java&quot; including=&quot;**/*.java&quot;/&gt;
  6. &lt;classpathentry kind=&quot;src&quot; path=&quot;src/main/resources&quot; excluding=&quot;**/*.java&quot;/&gt;
  7. &lt;classpathentry kind=&quot;output&quot; path=&quot;target/classes&quot;/&gt;
  8. &lt;classpathentry kind=&quot;con&quot; path=&quot;org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_121]&quot;/&gt;
  9. &lt;classpathentry kind=&quot;var&quot; path=&quot;M2_REPO/io/netty/netty-all/4.1.30.Final/netty-all-4.1.30.Final.jar&quot; sourcepath=&quot;M2_REPO/io/netty/netty-all/4.1.30.Final/netty-all-4.1.30.Final-sources.jar&quot;/&gt;
  10. &lt;classpathentry kind=&quot;var&quot; path=&quot;M2_REPO/javassist/javassist/3.12.1.GA/javassist-3.12.1.GA.jar&quot; sourcepath=&quot;M2_REPO/javassist/javassist/3.12.1.GA/javassist-3.12.1.GA-sources.jar&quot;/&gt;
  11. &lt;/classpath&gt;

答案1

得分: 1

你缺少了classpath。Eclipse 管理它自己的classpath,这就是为什么在Eclipse中可以工作。

创建一个可以通过 java -jar 直接执行的 jar 文件的最简单方法是使用 maven-shade-plugin。你可以查看文档,了解如何使用 maven-shade-plugin 创建可执行的jar文件。

更新
你需要配置要包含在shaded jar中的构件,可以参考这里的描述。例如,如果你想要包括所有依赖项,包括传递依赖,可以按照以下方式进行配置:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-shade-plugin</artifactId>
  4. <version>3.2.4</version>
  5. <configuration>
  6. <artifactSet>
  7. <includes>
  8. <include>*:*</include>
  9. </includes>
  10. </artifactSet>
  11. <shadedArtifactAttached>false</shadedArtifactAttached>
  12. </configuration>
  13. <executions>
  14. <execution>
  15. <phase>package</phase>
  16. <goals>
  17. <goal>shade</goal>
  18. </goals>
  19. <configuration>
  20. <transformers>
  21. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  22. <manifestEntries>
  23. <Main-Class>paplistener.PAPServer</Main-Class>
  24. <Build-Number>1</Build-Number>
  25. </manifestEntries>
  26. </transformer>
  27. </transformers>
  28. </configuration>
  29. </execution>
  30. </executions>
  31. </plugin>

你可以在文档页面 https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html 找到所有可用的配置项。

英文:

You're missing the classpath. Eclipse manages its own classpath, that's why it works in Eclipse.

The easiest way to create a jar file that can be executed simply by java -jar is to use a maven-shade-plugin. You can check the documentation for how to create an executable jar with maven-shade-plugin.

Update:
You need to configure which artifacts to include in the shaded jar as described here. For example if you want include all dependencies including transitive, you can do as following:

  1. &lt;plugin&gt;
  2. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  3. &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
  4. &lt;version&gt;3.2.4&lt;/version&gt;
  5. &lt;configuration&gt;
  6. &lt;artifactSet&gt;
  7. &lt;includes&gt;
  8. &lt;include&gt;*:*&lt;/include&gt;
  9. &lt;/includes&gt;
  10. &lt;/artifactSet&gt;
  11. &lt;shadedArtifactAttached&gt;false&lt;/shadedArtifactAttached&gt;
  12. &lt;/configuration&gt;
  13. &lt;executions&gt;
  14. &lt;execution&gt;
  15. &lt;phase&gt;package&lt;/phase&gt;
  16. &lt;goals&gt;
  17. &lt;goal&gt;shade&lt;/goal&gt;
  18. &lt;/goals&gt;
  19. &lt;configuration&gt;
  20. &lt;transformers&gt;
  21. &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;
  22. &lt;manifestEntries&gt;
  23. &lt;Main-Class&gt;paplistener.PAPServer&lt;/Main-Class&gt;
  24. &lt;Build-Number&gt;1&lt;/Build-Number&gt;
  25. &lt;/manifestEntries&gt;
  26. &lt;/transformer&gt;
  27. &lt;/transformers&gt;
  28. &lt;/configuration&gt;
  29. &lt;/execution&gt;
  30. &lt;/executions&gt;
  31. &lt;/plugin&gt;

You can see all available configuration in the doc's page https://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html

答案2

得分: 1

  1. <project>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-shade-plugin</artifactId>
  6. <version>3.2.4</version>
  7. <executions>
  8. <execution>
  9. <phase>package</phase>
  10. <goals>
  11. <goal>shade</goal>
  12. </goals>
  13. <configuration>
  14. <transformers>
  15. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  16. <mainClass>com.mycompany.app.App</mainClass>
  17. </transformer>
  18. </transformers>
  19. </configuration>
  20. </execution>
  21. </executions>
  22. </plugin>
  23. </plugins>
  24. </project>
英文:

The plugin element should not be inside build->pluginManagement->plugins, but inside build->plugins. Like this

  1. &lt;project&gt;
  2. &lt;plugins&gt;
  3. &lt;plugin&gt;
  4. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  5. &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
  6. &lt;version&gt;3.2.4&lt;/version&gt;
  7. &lt;executions&gt;
  8. &lt;execution&gt;
  9. &lt;phase&gt;package&lt;/phase&gt;
  10. &lt;goals&gt;
  11. &lt;goal&gt;shade&lt;/goal&gt;
  12. &lt;/goals&gt;
  13. &lt;configuration&gt;
  14. &lt;transformers&gt;
  15. &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;
  16. &lt;mainClass&gt;com.mycompany.app.App&lt;/mainClass&gt;
  17. &lt;/transformer&gt;
  18. &lt;/transformers&gt;
  19. &lt;/configuration&gt;
  20. &lt;/execution&gt;
  21. &lt;/executions&gt;
  22. &lt;/plugin&gt;
  23. &lt;/plugins&gt;
  24. &lt;/project&gt;

huangapple
  • 本文由 发表于 2020年10月27日 01:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64542206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定