I have a .jar file and I want to execute/debug it with Netbeans like I can do in IntelliJ. In IntelliJ it's possible to define a run configuration passing the parameters, like executing with java -jar parameters. It's possible to do this with Netbeans?
You can not run arbitrary classes from a jar file in NetBeans. You can only run classes that are part of your (current) project.
You would need to create a Java main class that calls the class you want to debug inside the jar file.
So if the class in the jar file is called some.package.Main you need to create a Java class:
package danyboy.debug;
public class RunIt
{
public static void main(String args[])
{
some.package.Main.main(args);
}
}
Then you can setup a run configuration inside NetBeans to start danyboy.debug.RunIt and pass the necessary parameters.
I assume the jar file is already part of your project's class path.
Related
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
I have a scala project with 2 objects that extend App. I have specified one as the main class in build.sbt. I'm using assembly to build a fat jar.
How can I execute the non-default main class when running the jar on the command line? I.e. if com.example.app1 is specified as the main class in build.sbt, how could I run com.example.app2 from the command line using the jar (assuming that also extends App)?
This will be in a production environment where I won't have sbt.
You run the default main class (from the jar's manifest) like this:
java -jar assembly.jar
And you would run a different main class like this:
java -cp assembly.jar com.example.app2
So I create a groovy jar but I'm not able to run it.
I'm using HelloWorld.groovy as an example
class HelloWorld {
static main(args) {
println("Hello World");
}
}
I save it as a jar like I would with any java file via export in eclipse/ggts:
right clicking groovy file in project explorer
click export
select jar File
specify jar file name
specify HelloWorld as the main class in the Manisfest file
keep all the defaults selected
click finish and create the jar
Now I try to run HelloWorld.groovy via java -jar HelloWorld.jar on my command line.
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
When I export it also allows me to export .classpath into my jar.
Shouldn't that include my groovy-all.jar that my GGTS is using?
Am I missing something?
I've been successful with using GroovyWrapper to create groovy jars. I want to see if it's possible with just my IDE.
I think that in eclipse you export an executable jar and that gives the option to package the dependencies?
I create the simplest maven project in eclipse. I add one JUnit test in src/test/java. Then I create a simple application (in src/main/java) that tries to do Class.forName("package.MyTestClass");. The class is not found even though the eclipse project is defined to export the src/test/java as a source folder.
What is going on? How can I fix this?
I wanted to do the following with TestNG
public static void main(String[] args) throws ClassNotFoundException {
// Class.forName("package.MyTestClass");
TestNG.main(new String[]{"testng.xml"});
}
It fails because it cannot find the test classes with:
[TestNG] [ERROR] Cannot find class in classpath: package.MyTestClass
If I have understood you correctly, your project is like this
/src/main/java/Main.java (or whatever you have called it)
/src/test/java/package/MyTest.java
In your Main class you are trying to create an instance of a test class. This will not work. The classes in your main directory are available to the classes in your test directory, not the other way around, which makes sense. Your test classes must know about your application to test it, but your application should not know about the test classes
I'm altering a hadoop map - reduce job that currently compiles and runs fine without my changes.
As part of the job, I will now be connecting to S3 to deliver a file.
I developed a (very simple) s3Connector class, tested and ran it in eclipse,
then went to hook it into my reduce job. In order to run the job in hadoop, I have to export the project as a jar file, then call it from hadoop. The jar file seems to compile and export without problem from eclipse, but when I run it in hadoop, I get a java.lang.VerifyError exception.
java.lang.VerifyError: (class: com/extrabux/services/S3Connector, method:
connectToS3 signature: ()V) Incompatible argument to function
Several other posts mention that there may be jar version dependencies that are conflicting, but in my eclipse build path, I added all the latest jar files for the specified libs, and pushed them to the top of the build path order.
This is about as simple as I can isolate it down to:
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.security.AWSCredentials;
public class S3Connector {
protected RestS3Service s3Service;
protected AWSCredentials awsCredentials;
public S3Connector()
{
this.awsCredentials= new AWSCredentials("my secret 1", "my secret 2");
}
public void connectToS3() throws Exception
{
this.s3Service = new RestS3Service(this.awsCredentials);
}
}
Even that simple class will die.. Same message. As soon as I comment out the AWS credentials in the constructor and RestS3Service, the issue disappears. Basically, I think it's some kind of library export problem out of eclipse, but not sure how to find it.
Figured this out. There was an old version of the jets3t jar that was in the hadoop lib dir
the hadoop command line script loops over all jars in the lib dir and physically adds them to the classpath on the final exec'ed command line command that it builds. This command line classpath of the 0.6.0 jar was overriding the good 0.8.0 jar that I was exporting in my jar file. Since the 0.6.0 version did not have the specified constructor for RestS3Service , the java.lang.VerifyError was getting thrown. By removing the 0.6.0 lib from hadoop, all was well.