如何从Hadoop上的java jar文件中运行一个类? [英] how to run a class from java jar file on Hadoop?

查看:481
本文介绍了如何从Hadoop上的java jar文件中运行一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从Eclipse导出的jar文件(Runnable JAR >>将所需的库复制到一个子文件夹中)。



在java中,如果将主类设置为manifest.xml可以运行:

  java -jar MyTest.jar参数
/ pre>

如果要在jar文件中运行另一个主类,或者您没有在manifest.xml中设置可以运行的主类:

  java -cp MyTest.jar MyOtherMainClass参数

在Hadoop中,如果在manifest.xml中设置了主类,则可以运行:

  hadoop jar MyTest.jar参数

如果输入:

  hadoop jar MyTest.jar MyOtherMainClass参数

它会考虑MyOtherMainClass作为参数(不能作为一个类运行)在jar的原始主类的args数组中。



现在,如果要运行另一个在jar文件中的主类是什么你会输入吗?



我期待类似的东西:

  hadoop java -cp MyTest.jar MyOtherMainClass参数

但是它给出了:

 错误:找不到或加载主类java 

$ b $注意:如果我从hadoop java -cp MyTest.jar MyOtherMainClass参数中删除hadoop,它将正常启动

解决方案

问题来自Eclipse强制您在jar文件中设置主类,从而阻止您运行所需的类。所有你需要做的是从jar文件的manifest.xml文件中删除主类,并运行:

  hadoop jar MyTest .jar MyOtherMainClass参数

看看这里:
http://www.roman10.net / 2012/07/26 / specified-the-main-class-in-a-jar-file-for-hadoop /



I键入相同的文本,以防网址被删除:



Hadoop支持执行jar文件。对于正常Java执行中的可执行jar文件,可以在命令行中指定主类,如上一篇文章所述:在jar文件中的主类之间切换。



但是,使用hadoop运行的可执行jar文件的规则有点不同。基本上,以下规则保持(我在Hadoop 1.0.3上测试),



如果jar文件包含其清单文件中指定的主类,hadoop将占用主类即使命令指定另一个主类。这与正常的java执行不同,我们可以指定一个主类来覆盖清单文件中的一个。
如果一个jar文件在清单文件中不包含一个主类,hadoop允许我们指定主类。
在eclipse中,当将项目导出为可运行的jar文件时,它总是在启动配置中要求一个主类。



所选的主类将被放置在清单文件中。以下是我的helloworld项目中的主要类设置为HelloWorld的META-INF / MANIFEST.MF文件的内容。



Manifest-Version:1.0
类路径:。
主类:hello.HelloWorld
可以使用文件提取器浏览jar文件,使用文件编辑器打开清单文件,只需删除最后一行即可删除主类配置,并保存出现提示时对jar文件的更改。这将创建一个没有主类的可运行的jar文件。



修改后的jar文件可以在用户提供的主类配置的Hadoop中使用,如下面的示例命令所示,



$ hadoop jar hello.jar hello.HelloWorld


I have a jar file exported from Eclipse (Runnable JAR>>Copy required libraries into a sub folder).

In java if you set the main class in the manifest.xml you can run:

java -jar MyTest.jar arguments

if you want to run another main class in the jar file or if you didn't set a main class in the manifest.xml you can run:

java -cp MyTest.jar MyOtherMainClass arguments

In Hadoop if main class is set in manifest.xml you can run:

hadoop jar MyTest.jar arguments

If you type:

hadoop jar MyTest.jar MyOtherMainClass arguments

It will consider MyOtherMainClass as argument (not as a class to run) in the "args" array of the original main class of jar.

Now if you want to run another main class in the jar file what will you type?

I expect something similar to:

hadoop java -cp MyTest.jar MyOtherMainClass arguments

but that gives:

Error: Could not find or load main class java

Notice: if I remove "hadoop" from "hadoop java -cp MyTest.jar MyOtherMainClass arguments" it will launch normally

解决方案

The problem comes from Eclipse forcing you to set the main class in the jar file and hence preventing you to run the class that you want. All you have to do is remove the main class from the manifest.xml file of the jar file and run:

hadoop jar MyTest.jar MyOtherMainClass arguments

Take a look here: http://www.roman10.net/2012/07/26/specify-the-main-class-in-a-jar-file-for-hadoop/

I typed the same text in case the url deleted:

Hadoop support execution of jar file. For an executable jar file in normal java execution, one can specify the main class in the command line, as covered in my previous post: switch between main classes in a jar file.

However, the rules are a bit different for executable jar file running with hadoop. Basically the following rules hold (I tested on Hadoop 1.0.3),

If a jar file contains a main class specified in its manifest file, hadoop will take the main class even if the command specify another main class. This is different from normal java execution where we can specify a main class to overwrite the one in the manifest file. If a jar file does not contain a main class in manifest file, hadoop allows us to specify the main class. At eclipse, when one export a project as runnable jar file, it always ask for a main class at Launch configuration.

The main class selected will be put in the manifest file. Below is the content of the META-INF/MANIFEST.MF file in my helloworld project where the main class is set to HelloWorld.

Manifest-Version: 1.0 Class-Path: . Main-Class: hello.HelloWorld One can browse the jar file using a file extractor, open the manifest file using a file editor, and simply delete the last line to remove the main class configuration, and save the changes to the jar file when prompted. This will create a runnable jar file without main class.

The modified jar file can then be used in Hadoop with user supplied main class configuration, as shown in the sample command below,

$ hadoop jar hello.jar hello.HelloWorld

这篇关于如何从Hadoop上的java jar文件中运行一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆