Fork me on GitHub

使用IDEA打jar包后出现“无法找到主类”的错误

使用IDEA打jar包后出现“无法找到主类”的错误

我在分别尝试使用Eclipse和IDEA编写mapreduce程序时,相同的环境与代码,IDEA打出的jar包会报“无法找到主类”的错误。网上找了下,别人说要加上一段指定主类的代码,可是我的原代码中已经写过了,对比了两边的pom文件,我怀疑是放置代码的位置有问题。

IDEA自动生成的pom中build是这样写的:

1
2
3
4
5
6
7
8
9
<build>
<pluginManagement>
<plugins>
<plaugin>
</plaugin>
...
</plugins>
</pluginManagement>
</build>

而Eclipse中没有<pluginManagement></pluginMagagement>这对标签,我原本是将代码放在<plugins></plugins>内部,但考虑到IDEA没有识别这一段代码,我做了修改如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<build>
<pluginManagement>
<plugins>
<plaugin>
</plaugin>
...
</plugins>
</pluginManagement>
<plugins>
<plugin>
(新增代码)
</plugin>
</plugins>
</build>

最后放上bulid内完整代码以供参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>
com.hadoop.mapreduce.WordCountMain
</Main-Class>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
欢迎投喂,但你的支持就是对我最佳的回馈。