Scala: Creating a small executable Jar relying on external Scala libraries - scala

I'm trying to package a small application (still learning Scala!) in a "clean way". The goal is to have an executable JAR file. I've done the following:
packaged a JAR using sbt -> will work with
scala -cp myjarfile.jar MyClass
or
java -classpath path\to\scala-library.jar;myjarfile.jar MyClass
but won't work with
java -jar myjarfile.jar
because then scala/ScalaObject cannot be found. And no use adding a classpath on this last one, since the -jar option will ignore the -classpath option. Note that I have added the scala libs in my system CLASSPATH variable, but it seems to be ignored too when -jar is used.
added the scala library contents (by unzipping them first) to the jar created by sbt. This then works (the jar can be double-clicked and launched), but the manipulation is somewhat lengthy, and the resulting file is several megabytes big. Not ideal.
After hours of googling, I can see no way to create a small jar file that will launch when double-clicked and that doesn't involve a painful manipulation. What am I missing? I'm guessing there should be some way to define where the scala libraries are at system level. How do you deal with small applications that you want to be compiled and ready-to-run for efficiency?
Note that while I'm using sbt, I don't have a jar tool at hand (I'm relying on a JRE, not a JDK).
Thanks!
Pierric.

The following setup works for me:
have scala-library.jar in the same folder as the executable jar (and call java from there)
put this into your manifest:
Class-Path: scala-library.jar
Another option is to merge the contents of scala-library.jar into your application jar. The drawback is that this will increase its size. But you can use Proguard to strip unused classes from your final jar. I think there is an easy way of using sbt to package an executable jar using proguard.
Regarding the shrinking using Proguard, you can have a look at this page. It's about Android development; just ignore this part and have a look at the tables of the shrinking results. Some example applications shrink to less than 100kB.
Edit
Maybe you need to refine your question a bit. What are you trying to achieve? Do you want to install the program only on your system or do you want to distribute it?
If all you want is quickly launching a Java application without much impact of the JVM start-up time you can have a look at nailgun.

Related

Generate a JAR from one Scala source file

I have no Scala experience, but I need to create a JAR to include on a project's classpath from a single Scala source file.
I'm thinking there is a relatively straightforward way to do this, but I can't seem to figure it out.
The Scala file is here: http://pastebin.com/MYqjNkac
The JAR doesn't need to be executable, it just needs to be able to be referenced from another program.
The most convenient way is to use some build tool like Sbt or Maven. For maven there is the maven-scala-plugin plugin, and for Sbt here is a tutorial.
If you don't want to use any build tool, you may want to compile the code with scalac and then create the jar file manually by using zip on the resulting class files and renaming it to jar. But you have to preserve the directory structure. In your pastebin you use the package org.apache.spark.examples.pythonconverters, so make sure the directories match.
Btw, if you want to just integrate this piece of code with your java project, and using maven, you can have the scala code in your 1 project as well (in src/main/scala). Just use the maven-scala-plugin plugin and hook it to the compile phase, or some sooner phase if your Java code depends on it. However, I don't recommend mixing multiple languages in one project, I would split it into two separate ones.

Have jre7; JAR is jre6 compiled; can't run

I built a JAR in NetBeans, setting the Source/Binary Format to JDK6. So it's compiled to jre6. I can use the JAR (outside the IDE), but if I send it to a machine with jre7, I have to alter the environment variable to get it to execute from the JAR.
Isn't Java backwards compatible? Shouldn't this work without effort on jre7? I am not doing -anything- fancy in this program. It's a few kilobytes, couple hundred lines, import on some Map classes from java.util.
Is there a command-line option for java to use an older runtime, or an annotation to write in, to identify the class files as version 6?
Embarrassed I can't find specs to answer these questions.

When I compile a file in IntelliJ on a maven project, what does it run?

I'm trying to make the move to VIM for my Java/Scala work. However it seems I cannot compile a single file using Maven. When developing I typically like to compile a file just to make sure it compiles before I compile the whole project.
In intellij I can choose to compile a single file that I'm on, what does that run behind the scenes so that my classpath is loaded with all my dependencies from maven?
The question boils down to what can I run from the command line to compile a file using Maven the fastest?
The fastest way to compile from IDEA is Build | Make. It will compile the changed files and all the dependencies. Compiling single file is also possible, IDEA will use Java Compiler API (like javac does) for such compilation.
IDEA doesn't compile using Maven in such cases.
I don't think single file is compiled using maven. I think IDEA just "knows" what what libraries are necessary (by parsing your pom.xml) and adds it to the classpath when running scalac.

How do I get my java program written with NetBeans to compile with javac?

I have two java files. They run fine in NetBeans, but I have to compile them in javac, and run them on a unix machine since I'm connecting to a database on my school's server.
I've been searching online but everything is too specific, and I'm not too familiar with NetBeans.
What I'm doing is copying those two java files, and a .form file to a directory on my school's server, and then try to compile the two java files using javac. However, I'm assuming that it doesn't compile because it's missing all the information from the .form file?
I'm getting 100 errors when compiling the one of the java files, and they look something like this:
CARTSJFrame.java:380: package org.jdesktop.layout does not exist
.add(jPanel9, org.jdesktop.layout.GroupLayout.PREFERRED_ SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLay
My guess was that it has something to do with the .form file that I can see in my NetBeans project directory.
I've looked at previous questions about this but still haven't been able to figure this out. Any help is appreciated.
You are running into compilation errors because NetBeans includes a jar in your project's classpath automagically when you compile and run the project.
NetBeans uses the dot-form file to help it generate code for the layout. It is not used at compilation time.
There are a couple strategies that you can follow to resolve this problem:
Get the jar that has the classes...
http://www.findjar.com/jar/net.java.dev.swing-layout/jars/swing-layout-1.0.1.jar.html;jsessionid=252692AC0FBE9421C9436A748744ACED... and include that jar in your
classpath at compile and runtime.
Convert the code in your project to
use the javax.swing.GroupLayout.
This is a 'standard part' of Java SE 6. This SO answer covers how to convert between
org.jdesktop.layout.GroupLayout and
javax.swing.GroupLayout in
NetBeans.
I think your problem is that javac doesn't know where to find that library.
A typical Java project uses many libraries. Netbeans uses a folder "lib" to store those libraries and also some configuration files to automatically set the classpath. The classpath is a environment variable that Javac uses to 'know' where are the libraries.
When you use javac to compile the java files you need to provide the CLASSPATH variable first. Write all you dependencies.
An example:
Project/compile_all.sh
export CLASSPATH=$CLASSPATH:"lib/jopt-simple-3.2.jar":"lib/commons-io-2.0.jar"
javac src/*.java
Now you just need to run
sh compile_all.sh
And it compiles all you .java files

How to compile standalone scala

Just starting to learn scala.. I can't seem to figure out how to compile something into a standalone application. I think I almost have a working .jar file, but keep getting a Main-Class error even though it's in the manifest,
Scala programs require at a minimum the scala-library.jar file that accompanied the Scala Development Kit whose compiler you used to compile the sources. This is in addition to any 3rd-party Java (or Scala) libraries you might use. So from the perspective of building a stand-alone application (i.e., one that can be launched with a java -jar ... command) you must treat the scala-library.jar like a 3rd-party library.