How to specify a different main class when running a scala jar on the command line? - scala

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

Related

how to run a class from java jar file on Hadoop?

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

running sbt class using only a jar

Is there any way to run sbt commands with only a jar instead of a project?
I've been having issues using scopt with java or scala commands, and it only seems to work with sbt.
Ideally something like
sbt --jar <jar name>/"run-main <options"
You'd probably want to package everything up into something you can execute. One possibility would be to create a fat jar using something like sbt-assembly.
Once you've built your jar, you can then:
java -jar /path/to.jar --your-options
Take note that at this point you can only do what would have been the equivalent of sbt run-main with the jar. You cannot of course invoke any of the other sbt commands on the jar created.

Jython can import lib when run in a single myapp-with-dependencies.jar but not when jython is separate

I have an application which consists of some Java classes and some Python scripts; I use Jython from my Java code to invoke the Python scripts.
My application is built using Maven, and produces an ordinary .jar file containing the compiled Java classes, and the Python scripts as resources. I also use maven-assembly-plugin to generate a myapp-with-dependencies.jar that contains the same plus bundles in the contents of the Jython dependencies.
When I run my application using the with-dependencies jar:
java -classpath target/myapp-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.example.myapp.Main
...it works as expected. But when I run it using two separate jars:
java -classpath "target/myapp-1.0.0-SNAPSHOT.jar:/Users/richard/.m2/repository/org/python/jython-standalone/2.5.3/jython-standalone-2.5.3.jar" com.example.myapp.Main
...it fails: when I ask Jython to execute "import mylib" I get an exception:
ImportError: No module named mylib
In both cases, the exact contents of the classpath should be completely identical, the only difference is that in the first case everything is in one jar, but in the second case it is split across two jars.
What could be causing this behaviour?

Run junit4 test from cmd

I tried to run junit4 test case from command line using:
java -cp junit-4.8.1.jar;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
but I got the following error:
java.lang.NoClassDefFoundError: graph/shortestgraphpath;
while the test case is working without any problems in eclipse.
Hint: in eclipse, shortestgraphpath was added in Referenced Libraries.
You need to the jar file containing shortestgraphpath to java class path.
java -cp junit-4.8.1.jar;test\Dijkstra; test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
The class path is the value that you pass to java with -cp so in your question you just supply junitand your compiled classes.
Try updating it with the jar file with the missing class.
java -cp junit-4.8.1.jar;<path to jar file>;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
You might have to add additional jar files as well. I recommend that you take a look at some build tool to help you build and run your java applications for example Maven, Gradle, Buildr.

Scala SBT: standalone jar

The answer: Making stand-alone jar with Simple Build Tool seems like what I need, but it did not have enough information for me, so this is a followup.
(1) How do I adapt the answer to my need? I don't understand what would need to be changed.
(2) What command do I run to create the standalone jar?
(3) Where can I find the jar after it has been created?
What I've tried:
Pasting the code in the linked answer verbatim into my: project/build/dsg.scala file. The file now has a
class ForkRun(info: ProjectInfo) extends DefaultProject(info)
(from before, used for running projects in a separate VM from SBT) and the new:
trait AssemblyProject extends BasicScalaProject
from the linked answer.
I also tried pasting the body (all defs and the lazy val of the AssemblyProject into the body of ForkRun.
To create a jar I ran package at the SBT prompt and get:
[info] Packaging ./target/scala_2.8.1/dsg_2.8.1-1.0.jar ...
[info] Packaging complete.
So I tried running the dsg_2.8.1-1.0.jar from the shell via:
java -jar dsg_2.8.1-1.0.jar
But I get:
Failed to load Main-Class manifest attribute from
dsg_2.8.1-1.0.jar
Could this be caused by having multiple entry points into my project? I select from a list when I execute run from the SBT prompt. Perhaps I need to specify the default when creating the package?
Here's a writeup I did on one way to make an executable jar with SBT:
http://janxspirit.blogspot.com/2011/01/create-executable-scala-jar-with-sbt.html
sbt-assembly is a sbt plugin to create a standalone jar of Scala sbt project with all of its dependencies.
Refer this post for more details with an example.