无法运行jar文件,因为“找不到或加载主类”

2j4z5cfb  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(576)

我有一个test dir,它包含3个文件夹:

  1. --META-INF:
  2. one file MANIFEST.MF in the dir:
  3. MANIFEST.MF:
  4. anifest-Version: 1.0
  5. Created-By: 1.7.0_04-ea (Oracle Corporation)
  6. Class-Path: lib/*; .
  7. Main-Class: Setup.WordCount
  8. -- lib:
  9. all the external jars I need for the project
  10. -- Setup:
  11. 3 files in the dir:
  12. WordCount$IntSumReducer.clas
  13. WordCount$TokenizerMapper.class
  14. WordCount.class

我使用以下命令创建一个jar文件

  1. jar cmf test.jar test/META-INF/MANIFEST.MF test/Setup test/lib

但是当我试着管理 test.jar ,报告了一个错误:

  1. Error: Could not find or load main class Setup.WordCount

我试了一整天调试这个问题,还是不知道! WordCount.java :

  1. package Setup;
  2. import java.io.IOException;
  3. import java.util.StringTokenizer;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.IntWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13. import org.apache.hadoop.util.GenericOptionsParser;
  14. public class WordCount {
  15. public static class TokenizerMapper
  16. extends Mapper<Object, Text, Text, IntWritable>{
  17. private final static IntWritable one = new IntWritable(1);
  18. private Text word = new Text();
  19. public void map(Object key, Text value, Context context
  20. ) throws IOException, InterruptedException {
  21. StringTokenizer itr = new StringTokenizer(value.toString());
  22. while (itr.hasMoreTokens()) {
  23. word.set(itr.nextToken());
  24. context.write(word, one);
  25. }
  26. }
  27. }
  28. public static class IntSumReducer
  29. extends Reducer<Text,IntWritable,Text,IntWritable> {
  30. private IntWritable result = new IntWritable();
  31. public void reduce(Text key, Iterable<IntWritable> values,
  32. Context context
  33. ) throws IOException, InterruptedException {
  34. int sum = 0;
  35. for (IntWritable val : values) {
  36. sum += val.get();
  37. }
  38. result.set(sum);
  39. context.write(key, result);
  40. }
  41. }
  42. public static void main(String[] args) throws Exception {
  43. Configuration conf = new Configuration();
  44. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  45. if (otherArgs.length != 2) {
  46. System.err.println("Usage: wordcount <in> <out>");
  47. System.exit(2);
  48. }
  49. Job job = new Job(conf, "word count");
  50. job.setJarByClass(WordCount.class);
  51. job.setMapperClass(TokenizerMapper.class);
  52. job.setCombinerClass(IntSumReducer.class);
  53. job.setReducerClass(IntSumReducer.class);
  54. job.setOutputKeyClass(Text.class);
  55. job.setOutputValueClass(IntWritable.class);
  56. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  57. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  58. System.exit(job.waitForCompletion(true) ? 0 : 1);
  59. }
  60. }

代码与 hadoop/example/WordCount ,我尝试在本地开发环境中实现hadoop示例。

pxyaymoc

pxyaymoc1#

你的jar语句有点不对劲。试试这个:

  1. jar -cfm test.jar test/META-INF/MANIFEST.MF -C test Setup -C test lib

您的命令将test/setup/wordcount.class放在jar中,这就是为什么java找不到setup/wordcount.class
您发布的代码中也缺少package语句:

  1. package Setup;
h79rfbju

h79rfbju2#

在代码中指定包,然后在start命令中使用它/例如:

  1. hadoop jar mywordcount.jar hadoop.mytest.WordCount input22/input /home/training/output

哪里 hadoop.mytest.WordCount -这是你的包裹吗

相关问题