sbt-proguard issue with Java 1.8 - sbt

I'm trying to get smaller scalar executable jar file with sbt-proguard.
I added project/plugin.sbt these two lines of code:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-proguard" % "0.2.2")
The first one is to get uberjar file, and I could get uberjar with sbt assembly that works fine.
Then, I executed sbt proguard:proguard to get this error message.
[error] Error: Can't read [/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/rt.jar] (Can't process class [apple/applescript/AppleScriptEngine.class] (Unsupported class version number [52.0] (maximum 51.0, Java 1.7)))
java.lang.RuntimeException: Proguard failed with exit code [1]
at scala.sys.package$.error(package.scala:27)
...
at java.lang.Thread.run(Thread.java:745)
[error] (proguard:proguard) Proguard failed with exit code [1]
From the hint from this post: ProGuard says Unsupported class version number [52.0] (maximum 51.0, Java 1.7) with sbt-proguard,
I switched to both Java 1.7 and Java 1.6 with export JAVA_HOME=/usr/libexec/java_home -v '1.6*' command to run proguard to get the slim-lined jar file, but this doesn't run.
Invalid or corrupt jarfile target/scala-2.11/proguard/myproject_2.11-1.0.jar
What might be wrong? These are the lines that are added to build.sbt.
proguardSettings
ProguardKeys.options in Proguard ++= Seq("-dontnote", "-dontwarn", "-ignorewarnings")
ProguardKeys.options in Proguard += ProguardOptions.keepMain("core.HelloWorld")

I believe this is documented in the pro guard docs.
Running your application with java -classpath <jarpath> --class classname <program-arguments> should work.
This happens because pro guard by default removes all MANIFEST files from the jar hence the java runtime cannot find the jar class entries. Another way to do this would be to keep the MANIFEST.md file and run it using the java -jar option but I have never tried that.

Define a recent Proguard version that supports Java 1.8
ProguardKeys.proguardVersion in Proguard := "5.3.3"
Also a couple of useful ones if you run out of mem are
javaOptions in (Proguard, ProguardKeys.proguard) := Seq("-Xmx2G")
javaOptions in (Proguard, ProguardKeys.proguard) += "-Xss1G"

Related

OPAL-How to build code in Docker in local machine (faced compile error)?

I access code in Docker($docker pull mreif/fse2016:evaluation), the code could be compiled and run without errors in remote server. While i download it to local machine, i suffered some errors in compiling(using: sbt compile):
[error] (*:update) sbt.ResolveException: unresolved dependency: de.opal-project#abstract-interpretation-framework_2.11;0.9.0-SNAPSHOT: not
[What i have done] I added follow lines into "build.sbt":
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
[error] evaluation/src/main/scala/org/opalj/evaluation/EntryPointAndCallEdgeCountAnalysis.scala:90: not found:
[What i have done] I added follow lines into "build.sbt":
libraryDependencies += "de.opal-project" % "fixpoint-computations-framework-analyses_2.11" % "0.9.0- SNAPSHOT"
3.[error] /src/main/scala/org/opalj/evaluation/EntryPointAndCallEdgeCountAnalysis.scala:130: not found: value LibraryEntryPointsAnalysis
I have checked the related code, LibraryEntryPointsAnalysis has been actually imported but doesn't work.
Could you please help me to confirm is there any operations i missed for compile the source code?
Thank you very much!
Jiang
The reason why it is not working is a version mismatch of the OPAL framework. The reason why it doesn't find the "LibraryEntryPointAnalysis" is, that it has been renamed.
You have to options:
Use the version of OPAL that is used in the Docker container
make a check out of OPAL at from version tag "ArtifactEvaluationFSE2016"
copy OPAl from the container like you did with the evaluation project
Adapt the Evaluation Project to the new API
the LibraryEntryPointsAnalysis is now called EntryPointAnalysis
there are probably other breaking changes that you have to fix
If you want to go with option one you have to build OPAL on your own because the eval version is not published on maven.

error javac: invalid flag: -J-Xmx1028m in sbt

I have OutOfMemoryError when I compile my project using sbt. So I need to enlarge the java compiler heap space. I use java 6. And I have set JAVA_HOME to java 6 in my bash env.
I tried to add this line to build.sbt
javacOptions ++= Seq(<other arguments>,"-J-Xmx1028m")
When I compile, I have error like this:
[error] javac: invalid flag: -J-Xmx1028m
[error] Usage: javac <options> <source files>
[error] use -help for a list of possible options
The funny thing is: this error disappears when I specify the javaHome in build.sbt, which I have already done it in my bash env:
javaHome := Some(file("path to java 6"))
Does it mean that I have to specify JAVA_HOME twice? once in bash env, once in build.sbt? I don't want to specify a absolute path in build.sbt, which makes it may not work properly on other computers.

How to run jar generated by package (possibly with other jars under lib)?

How can I run .jar file which is generated by sbt's package?
I created a really simple example with a single .scala source:
package org.pack {
class ScalaParser(files: Array[String]) {
def doAll() = {
println("hello")
}
}
object Main {
def main(args: Array[String]): Unit = {
val sp = new ScalaParser(args)
sp.doAll()
}
}
}
After running
$ sbt
> compile
> package
.jar is created in /target/scala-<version>. If I try to run it, it fails giving this error:
$ java -jar package_2.9.2-0.1.jar
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.pack.Main.main(Main.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
Note that no external libraries are used and sbt compile run works fine.
I attached the whole project including generated .jar on dropbox.
What is really weird is the fact that .jar Manifest contains right class to load, i.e. org.pack.Main. Maybe it is caused by something else.
System info
$ java -version
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
$ scala -version
Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL
Additional question - what if I had some external .jars in /lib? How can I assure that they are packed? I need one .jar runnable on (possibly) every JVM.
Thanks for help.
You can use the sbt plugin sbt-assembly:
sbt-assembly >= 0.12.0 with sbt >= 0.13.6
Since sbt-assembly is now an auto plugin, it is sufficient to add project/assembly.sbt to your sbt project:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
sbt-assembly 0.11
Add project/assembly.sbt to your sbt project:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
Add assembly.sbt as well:
import AssemblyKeys._ // put this at the top of the file
assemblySettings
Usage
This gives you another sbt command:
sbt assembly
which produces a "fat jar" (which includes all dependencies, including the Scala libraries).
Now you can start your program
java -cp .../package-assembly.jar
so you only need a Java installation and the "fat jar".
For those who do not wish to build a fat jar, all dependencies will have been downloaded as part of a successful run of sbt package. Classpath information is stored in a file within the target directory, which can be passed into the java command.
Example:
sbt package
java -cp target/scala-2.11/yourproject.jar:$(cat target/streams/compile/dependencyClasspath/\$global/streams/export) Main
Try
scala package_2.9.2-0.1.jar
Update
From Java the command would look like this,
java -cp $SCALA_HOME/lib/scala-library.jar -jar package_2.9.2-0.1.jar
Yet in the classpath you may need to add additional jar's, for instance -cp "jar1.jar:jar2.jar"

How to add scalax.io as dependency in SBT?

I want to use scalax.io._ to manipulate file operations with SBT.
When I ran it, I got the error message showing scalax is not found.
>sbt run
import scalax.io._
[error] ^
[error] iotest.scala:49: not found: object scalax
How to find the library dependency for this particular one?
A more general question, how to obtain the library dependency information for any library?
For example, if I need use actor in scala, I need specify a library dependency. How to find the library dependency?
"the library dependency information for any library" is part of the library's documentation and the author(s) is supposed to publish the info for different project management tools, sbt including. After all, what would be the purpose of developing a library that's hard to use?
Use http://search.maven.org/ to search for a library, and when you search for scala-io you'll get a list of available scala-io libraries.
Since I've never worked with the library I copied the ScalaIOExample example from Scala IO Documentation to have a working example. It needs the scalax.io and scalax.file packages that are distributed as scala-io-file artifact. Searching for the artifact leads to Artifact Details For com.github.scala-incubator.io:scala-io-file_2.10:0.4.2 with information on how to use it with Scala SBT in Dependency Information section.
With this, I created the following build.sbt in a sbt project:
scalaVersion := "2.10.3"
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-file" % "0.4.2"
It will add scala-io-file_2.10-0.4.2.jar to classpath and executing run in the project gives the following results:
$ sbt run
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Loading project definition from /Users/jacek/sandbox/stackoverflow/sbt-scala-io/project
[info] Set current project to sbt-scala-io (in build file:/Users/jacek/sandbox/stackoverflow/sbt-scala-io/)
[info] Running ScalaIOExample
Not interrupting system thread Thread[Keep-Alive-Timer,8,system]
Not interrupting system thread Thread[Keep-Alive-SocketCleaner,8,system]
[success] Total time: 5 s, completed Dec 31, 2013 11:16:42 PM

Specifying a relative source directory

I have just recently forked an outdated SBT project (which you can find here https://github.com/mdedetrich/sbt-bower) that was last working with an outdated version of SBT 0.11.0. I am trying to get the sbt plugin to work with SBT versions 0.12.3 and 0.13.0, however I am getting an issue on this line
https://github.com/mdedetrich/sbt-bower/blob/master/src/main/scala/SbtBowerPlugin.scala#L37
When I run sbt compile, I get the following error
[error] /Users/mdedetrich/github/sbt-bower/src/main/scala/SbtBowerPlugin.scala:37: value / is not a member of java.io.File
[error] sourceDirectory in Bower <<= (sourceDirectory).apply (_ / "main" / "webapp" )
Does anyone know what is causing this issue?
Thanks to Mark Harrah, this is actually due to this https://groups.google.com/forum/#!topic/simple-build-tool/0k1No2kfvm8/discussion
I had to remove the import Path._ in the SbtBowerPlugin.scala file and it now compiles fine!

Resources