Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的maven构建没有发现Netty';在执行时是什么样的EventLoopGroup类?_Java_Maven_Netty - Fatal编程技术网

Java 为什么我的maven构建没有发现Netty';在执行时是什么样的EventLoopGroup类?

Java 为什么我的maven构建没有发现Netty';在执行时是什么样的EventLoopGroup类?,java,maven,netty,Java,Maven,Netty,我按照Netty的文档编写代码,然后执行: mvn package 它构建成功。然后我跑: java -jar target/netty-listener-0.0.1-SNAPSHOT.jar 它向我提示了一个错误: 线程“main”java.lang.NoClassDefFoundError中出现异常: io/netty/channel/EventLoopGroup位于 位于的java.lang.Class.getDeclaredMethods0(本机方法) privateGetDecla

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

mvn package
它构建成功。然后我跑:

java -jar target/netty-listener-0.0.1-SNAPSHOT.jar
它向我提示了一个错误:

线程“main”java.lang.NoClassDefFoundError中出现异常: io/netty/channel/EventLoopGroup位于 位于的java.lang.Class.getDeclaredMethods0(本机方法) privateGetDeclaredMethods(Class.java:2701)位于 privateGetMethodRecursive(Class.java:3048)位于 java.lang.Class.getMethod0(Class.java:3018)位于 java.lang.Class.getMethod(Class.java:1784)位于 launcher.LaunchHelper.validateMainClass(LaunchHelper.java:544) 在 launcher.LaunchHelper.checkAndLoadMain(LaunchHelper.java:526) 原因:java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup位于 java.net.URLClassLoader.findClass(URLClassLoader.java:381)位于 loadClass(ClassLoader.java:424)位于 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)位于 loadClass(ClassLoader.java:357)。。。还有7个

据我所知,JRE并没有在执行时找到Netty的EventLoopGroup类。在Eclipse中,我在Maven的dependency选项卡上看到了netty all依赖项,EventLoopGroup就在那里。我尝试过多次更改Netty的版本,但都不起作用

这是我的主要代码:

package paplistener;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class PAPServer 
{
    
    private int port;
    
    public PAPServer(int port)
    {
        this.port = port;
    }

    public void run() throws Exception
    {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new CommandDecoder(),
                                            new PAPServerHandler());
                    }
                })
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);
            
            ChannelFuture f = b.bind(port).sync();
            
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception
    {
        int port = 8080;
        
        if(args.length > 0)
        {
            port = Integer.parseInt(args[0]);
        }
        
        new PAPServer(port).run();
    }
    
}

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


您缺少类路径。Eclipse管理自己的类路径,这就是它在Eclipse中工作的原因

创建可由
java-jar
简单执行的
jar
文件的最简单方法是使用
maven shade插件。您可以查看如何使用maven shade插件创建可执行jar

更新: 您需要配置要包括在着色jar中的工件,如所述。例如,如果希望包含所有依赖项(包括可传递项),可以执行以下操作:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <configuration>
       <artifactSet>
          <includes>
             <include>*:*</include>
          </includes>
       </artifactSet>
       <shadedArtifactAttached>false</shadedArtifactAttached>
    </configuration>
    <executions>
        <execution>
        <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>paplistener.PAPServer</Main-Class>
                            <Build-Number>1</Build-Number>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
 </plugin>

org.apache.maven.plugins

插件元素不应位于内部构建->插件管理->插件,而应位于内部构建->插件。像这样

<project>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.mycompany.app.App</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</project>

org.apache.maven.plugins
maven阴影插件
3.2.4
包裹
阴凉处
com.mycompany.app.app

我按你说的做了,但问题仍然存在。我编辑了我的帖子以显示新的pom.xml行。我不知道类路径是否有问题,因为maven构建(编译)所有东西。我不知道为什么在执行过程中会出现问题我必须手动添加工件(依赖项)吗?我试图包括工件集。我用*:*。然后我试过爱娥。两个都不起作用。我真的不知道为什么会发生这种情况。你能分享一下我编辑的帖子的输出吗。它似乎正在将netty复制到jar文件中。发布了.classpath,它也工作了!谢谢最后,Yuri是对的,但我需要在pluginManagement标签之外编写插件。
<artifactSet>
    <includes>
            <include>io.netty:*</include>
    </includes>
</artifactSet>
    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
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_121]"/>
  <classpathentry kind="var" path="M2_REPO/io/netty/netty-all/4.1.30.Final/netty-all-4.1.30.Final.jar" sourcepath="M2_REPO/io/netty/netty-all/4.1.30.Final/netty-all-4.1.30.Final-sources.jar"/>
  <classpathentry kind="var" path="M2_REPO/javassist/javassist/3.12.1.GA/javassist-3.12.1.GA.jar" sourcepath="M2_REPO/javassist/javassist/3.12.1.GA/javassist-3.12.1.GA-sources.jar"/>
</classpath>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <configuration>
       <artifactSet>
          <includes>
             <include>*:*</include>
          </includes>
       </artifactSet>
       <shadedArtifactAttached>false</shadedArtifactAttached>
    </configuration>
    <executions>
        <execution>
        <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>paplistener.PAPServer</Main-Class>
                            <Build-Number>1</Build-Number>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
 </plugin>
<project>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.mycompany.app.App</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</project>