I am new to learn maven by following this example given by sonatype's book.This is a simplest pom.xml file (works fine). But my question is, how does maven (mvn install) find Java compiler (javac) installed on my machine? This POM file doesn't specify JDK in any way. Thanks for explanation.
EDIT:
As a comparison, I am following next example in the book and this time, I received a compilation error because it can't find javax.My java file is in, as suggested in the book: src/main/java/org/sonatype/mavenbook/web/SimpleServlet.java
Now as I run mvn compile, and i got error message below. Why now maven can't find the compiler:
ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project simple-webapp: Compilation failure: Compilation failure:
[ERROR] /home/abigail/study/simple-webapp/src/main/java/org/sonatype/mavenbook/web/SimpleServlet.java:[4,1] package javax.servlet does not exist
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven- v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.simpleweb</groupId>
<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you go to this path, say with maven version 3.2.3
apache-maven-3.2.3-bin\apache-maven-3.2.3\bin
opne the mvn.bat, you can see below
#REM Maven2 Start Up Batch script
#REM Required ENV vars:
#REM JAVA_HOME - location of a JDK home dir
Clearly it picks JAVA_HOME, from your environment variables. Below code snippet from the file.
#REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto chkMHome
:chkMHome
if not "%M2_HOME%"=="" goto valMHome
for issue, javax.servlet
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
or whichever available
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
Maven uses the environment variable JAVA_HOME to find the JDK.
Since maven itself is a java app, it would just default to the jdk that runs the maven goal.
For more compilation options, you'd have to use:
http://maven.apache.org/plugins/maven-compiler-plugin/
Take a look at this also:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
EDIT:
To your followup question:
If you look in mvn executable (by default in: /usr/bin/mvn), you'd see it is a rather lengthy bash scriptie that spends a significant amount of time finding where the jdk is. Of course, you can set the JAVA_HOME system property and "help" the script, but even if you don't have it, it is going to attempt finding it, and only when it fails, it is going to spew an error.
From the /usr/bin/mvn:
if [ -z "$JAVA_HOME" ]; then
javaExecutable="`which javac`"
if [ -n "$javaExecutable" -a ! "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=`which readlink`
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
javaExecutable="`readlink -f \"$javaExecutable\"`"
javaHome="`dirname \"$javaExecutable\"`"
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
And
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
Actually, most of the code of the script is concerned with determining which java to use and where that java is located. However, once you run the mvn executable, that doesn't limit it to using the same jdk for compilation/running that it itself is run with. Herein is where the mvn compiler plugin comes into play, where you can specify additional jdk installations to use.
So i just got maven to pick up different Java location from my default:
Java8
[tomek:~] $ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
but maven picked up Java9
[tomek:~] $ mvn -version
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 9-internal, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-9-openjdk-amd64
JAVA_HOME was not set:
[tomek:~] $ echo "Java HOME: $JAVA_HOME"
Java HOME:
So I suspect it just bring first alternative on ubuntu (ignoring users choice):
[tomek:~] $ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority
Status
------------------------------------------------------------
0 /usr/lib/jvm/java-9-openjdk-amd64/bin/java 1091 auto mode
* 1 /opt/java/jdk1.8.0_144/bin/java 1 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
3 /usr/lib/jvm/java-9-openjdk-amd64/bin/java 1091 manual mode
Related
I am trying to use jmxterm with Eclipse Temurin Java 17, but because of the module encapsulation changes in Java 17, jmxterm no longer has access to the class sun.tools.jconsole.LocalVirtualMachine in the jdk.jconsole module. So, running the following command:
printf "open 25522\n" | java -jar jmxterm-1.0.4-uber.jar -v verbose
outputs the following exception:
java.lang.reflect.UndeclaredThrowableException
at jdk.proxy2/jdk.proxy2.$Proxy6.getAllVirtualMachines(Unknown Source)
at org.cyclopsgroup.jmxterm.jdk6.Jdk6JavaProcessManager.get(Jdk6JavaProcessManager.java:28)
at org.cyclopsgroup.jmxterm.SyntaxUtils.getUrl(SyntaxUtils.java:41)
at org.cyclopsgroup.jmxterm.cmd.OpenCommand.execute(OpenCommand.java:64)
at org.cyclopsgroup.jmxterm.cc.CommandCenter.doExecute(CommandCenter.java:161)
at org.cyclopsgroup.jmxterm.cc.CommandCenter.doExecute(CommandCenter.java:134)
at org.cyclopsgroup.jmxterm.cc.CommandCenter.execute(CommandCenter.java:176)
at org.cyclopsgroup.jmxterm.boot.CliMain.execute(CliMain.java:149)
at org.cyclopsgroup.jmxterm.boot.CliMain.main(CliMain.java:41)
Caused by: java.lang.IllegalAccessException: class org.cyclopsgroup.jmxterm.utils.WeakCastUtils$2 cannot access class sun.tools.jconsole.LocalVirtualMachine (in module jdk.jconsole) because module jdk.jconsole does not export sun.tools.jconsole to unnamed module #3bb50eaa
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Method.invoke(Method.java:560)
at org.cyclopsgroup.jmxterm.utils.WeakCastUtils$2.invoke(WeakCastUtils.java:136)
... 9 more
To fix this, I thought I would have to recompile jmxterm with the --add-exports option, so to do that, I added this plugin to the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>8</source>
<target>17</target>
<compilerArgs>
<arg>--add-exports</arg>
<arg>jdk.jconsole/sun.tools.jconsole=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
and I also had to add this config to the surefire plugin:
<argLine>
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.management/javax.management.openmbean=ALL-UNNAMED
</argLine>
I have verified that Maven is using the correct JDK to build jmxterm:
$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 17.0.4.1, vendor: Eclipse Adoptium, runtime: /usr/lib/jvm/temurin-17-jdk
Default locale: en_CA, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"
So with these changes I was able to build a new jmxterm uber jar with mvn package.
I then tried to run my new jmxterm jar with --add-exports using this command:
printf "open 25522\n" | java -jar jmxterm/target/jmxterm-1.0.4-uber.jar --add-exports jdk.jconsole/sun.tools.jconsole=ALL-UNNAMED -v verbose
but it still threw the same exception as above.
Other things I have tried that also didn't work:
Using --add-opens instead of --add-exports in the java command even though I don't think that should be necessary
Setting the source to 17 in pom.xml
Adding --add-modules jdk.jconsole argument to the java command as well as to pom.xml
Is there something I am missing or is this just not possible?
In the command:
printf "open 25522\n" | java -jar jmxterm/target/jmxterm-1.0.4-uber.jar --add-exports jdk.jconsole/sun.tools.jconsole=ALL-UNNAMED -v verbose
You are passing the --add-exports flag as a program argument, rather than a VM argument.
The flag should be passed before the -jar flag instead:
printf "open 25522\n" | java --add-exports jdk.jconsole/sun.tools.jconsole=ALL-UNNAMED -jar jmxterm/target/jmxterm-1.0.4-uber.jar -v verbose
I have a Java / Maven project that I build at home with Windows and was executing checkstyle properly. It's using the builtin ruleset, but I tried an external file as well.
Checking out the same code / pom.xml it doesn't seem to work with macOS. The odd thing is if I use the sun_checks.xml it's working fine. Using 8.8 didn't make a difference.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.7</version>
</dependency>
</dependencies>
<configuration>
<encoding>UTF-8</encoding>
<configLocation>google_checks.xml</configLocation>
</configuration>
</plugin>
If I run it like that, it will just complete without doing any checks:
mvn checkstyle:check -X
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T18:58:13+11:00)
Maven home: /gdev/apache-maven
Java version: 1.8.0_161, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
Default locale: en_AU, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.3", arch: "x86_64", family: "mac"
[..]
[DEBUG] The resource 'google_checks.xml' was found as jar:file:/Users/udoheld/.m2/repository/com/puppycrawl/tools/checkstyle/8.7/checkstyle-8.7.jar!/google_checks.xml.
[DEBUG] headerLocation LICENSE.txt
[DEBUG] JarResourceLoader : trying to load "jar:file:/Users/udoheld/.m2/repository/com/puppycrawl/tools/checkstyle/8.7/checkstyle-8.7.jar"
[..]
[DEBUG] Unable to process header location: LICENSE.txt
[DEBUG] Checkstyle will throw exception if ${checkstyle.header.file} is used
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
If I run the same with the sun checks:
[..]
[DEBUG] The resource 'sun_checks.xml' was found as jar:file:/Users/udoheld/.m2/repository/com/puppycrawl/tools/checkstyle/8.7/checkstyle-8.7.jar!/sun_checks.xml.
[DEBUG] headerLocation LICENSE.txt
[DEBUG] JarResourceLoader : trying to load "jar:file:/Users/udoheld/.m2/repository/com/puppycrawl/tools/checkstyle/8.7/checkstyle-8.7.jar"
[..]
[DEBUG] Checkstyle will throw exception if ${checkstyle.header.file} is used
[INFO] There are 7 errors reported by Checkstyle 8.7 with sun_checks.xml ruleset.
[ERROR] src/main/java/com/udoheld/Test.java:[0] (javadoc) JavadocPackage: Missing package-info.java file.
Trying the checkstyle on the commandline without using maven seems to work as well java -jar ~/Downloads/checkstyle-8.8/checkstyle-8.8-all.jar -c ../checkstyle-google.xml *:
Starting audit...
[WARN] /gdev/git/test/src/main/java/com/udoheld/Test.java:18: Comment has incorrect indentation level 4, expected is 6, indentation should be the same level as line 19. [CommentsIndentation]
As the logs show I'm using Maven 3.5.2 on macOS 10.13.3 with Java 1.8.0_161 64 Bit.
How do I get the maven-checkstyle-plugin working with the google ruleset on macOS?
It does actually run it but does not print the results. Change your plugin's configuration like below.(enabling console output)
<configuration>
<!-- <violationSeverity>warning</violationSeverity> -->
<consoleOutput>true</consoleOutput>
<encoding>UTF-8</encoding>
<configLocation>google_checks.xml</configLocation>
</configuration>
If you want to fail the build when check style finds warnings, uncomment the violationSeverity line. But it does not print total number of errors found, only prints each error it finds.
I migrated my project from Java 8 to Java 9. Now when I run maven install on this project inside Intellij Idea it fails with the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project tutorial: Fatal error compiling: tools.jar not found: C:\Program Files\Java\jdk-9.0.1..\lib\tools.jar -> [Help 1]
Idea runs the following command:
"C:\Program Files\Java\jdk-9.0.1\bin\java" "-Dmaven.multiModuleProjectDirectory=C:\Users\User\workspace\tutorial" -Dmaven.home=C:\Users\User\install\apache-maven-3.3.9 -Dclassworlds.conf=C:\Users\User\install\apache-maven-3.3.9\bin\m2.conf "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.2\lib\idea_rt.jar=63961:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.2\lib\idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper C:\Users\User\AppData\Local\Temp\idea_classpath org.codehaus.classworlds.Launcher -Didea.version=2017.3.2 -s C:\Users\User\.m2\settings.xml install -DskipTests -P localhost
It works fine if I run mvn install from Windows command line.
All settings point to Java 9:
C:\Users\User>java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
C:\Users\User>echo %JAVA_HOME%
C:\Program Files\Java\jdk-9.0.1
C:\Users\User>echo %JRE_HOME%
C:\Program Files\Java\jre-9.0.1
C:\Users\User>echo %IDEA_JDK%
C:\Program Files\Java\jdk-9.0.1
C:\Users\User>echo %IDEA_JDK_64%
C:\Program Files\Java\jdk-9.0.1
In Idea the following settings all point to Java 9:
Project Structure->Project Settings->Modules->Sources->Language level (Picture)
Project Structure->Project Settings->Modules->Dependencies->Module
SDK (Picture)
File->Settings->Build, Execution, Deployment->Build
Tools->Maven->Runner->JRE (Picture)
Relevant fragments of my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<testSource>${java.version}</testSource>
<testTarget>${java.version}</testTarget>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<meminitial>${maven-compiler-meminitial}</meminitial>
<maxmem>${maven-compiler-maxmem}</maxmem>
<compilerArgs>
<arg>--add-modules</arg>
<arg>java.xml.bind</arg>
<arg>--add-modules</arg>
<arg>java.se.ee</arg>
</compilerArgs>
</configuration>
</plugin>
<properties>
<java.version>9</java.version>
<maven-compiler-meminitial>512m</maven-compiler-meminitial>
<maven-compiler-maxmem>1024m</maven-compiler-maxmem>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
</properties>
I would be grateful for any tips what could be wrong.
Update
I also noticed, that Idea shows JAVA home as pointing to 1.8 in settings (Picture), while environment variables point to 1.9 (Picture).
In case anyone has similar problem, I was able to fix it by doing the following steps:
Installed 2018.1 Release Candidate version (suggested on JetBrains forum).
Changed Maven
installation in Idea to Bundled.
Deleted .idea directory in my
project and reimported it as maven project.
On startup, intellij determines the JDK in this way. see idea.bat file in the intellij installation directory (bin)
:: Locate a JDK installation directory which will be used to run the IDE.
:: Try (in order): IDEA_JDK, idea%BITS%.exe.jdk, ..\jre, JDK_HOME, JAVA_HOME.
Hope this helps
I am having the below issue when I run my maven build in eclipse. I saw many posts like this but i still can't solve this problem .Mine may be a little more specific, I know that my JAVA_HOME is pointed to java6, and that is because I am working on another application which uses java 6. The application in which I am running into issues uses Java 8
[ERROR] COMPILATION ERROR : [INFO] -------------------------------------------------------------
[ERROR] Unable to locate the Javac Compiler in: C:\Program Files\Java\jre1.8.0_92\..\lib\tools.jar Please ensure you are using JDK 1.4 or above and not a JRE (the com.sun.tools.javac.Main class is required). .In most cases you can change the location of your Java installation by setting the JAVA_HOME environment variable. [INFO] 1 error
<br>[INFO] -------------------------------------------------------------
<br>[INFO] ------------------------------------------------------------------------
<br>[INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------
<br>[INFO] Total time: 11:53 min [INFO] Finished at: 2016-08-18T16:52:13+05:30
<br>[INFO] Final Memory: 11M/141M
<br>[INFO] ------------------------------------------------------------------------
<br>[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project
Compilation failure [ERROR] Unable to locate the Javac Compiler in: [ERROR] C:\Program Files\Java\jre1.8.0_92\..\lib\tools.jar
[ERROR] Please ensure you are using JDK 1.4 or above and [ERROR] not a JRE (the com.sun.tools.javac.Main class is required).
<br>[ERROR] In most cases you can change the location of your Java [ERROR] installation by setting the JAVA_HOME environment variable.
[ERROR] -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
How can I resolve this problem without installing java 8 as it may may make changes to my JAVA_HOME etc which would make my other application to not run. Is there a way to get the JDK folder without running the .exe (p.s I already tried the 7zip method)
Please suggest
If you have the jdk8 then you can open cmd, run the command in this window to modify the JAVA_HOME set JAVA_HOME=<jdk8 path>. This will modify the JAVA_HOME env variable only for this instance of the cmd session and not for the whole OS. Now run the maven build command from this window and it wont fail.
If you wanna run the build from eclipse, then in eclipse try going to Windows --> Preferences --> Java --> Installed JREs --> click Add button --> Sel Standard VM and click next --> put in values for 'JRE Home' as path till the jdk folder and 'JRE Name' as java8 or whatever you wish --> click Finish.
Now right click the project of you interest --> click properties --> Java Compiler --> Tick 'Enable project specific settings' --> and select 1.8 here.
If 1.8 is not visible here, go to Java Build Path --> Add Library --> JRE System Library --> Alternate JRE --> select jdk8 --> Finish
Looking at the path in that error (C:\Program Files\Java\jre1.8.0_92\..\lib\tools.jar) you've installed the Java Runtime Environment (JRE).
To compile java you need the Java Development Kit (JDK) which contains the tools the maven plugin needs.
You can change your maven-compiler-plugin configurations to your JAVA_HOME.
Just go to eclipse window->preferences->type maven->and you could see something like template click on that and look for javac compiler, just go there and change to whatever java version you have.
<configuration>
<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
<source>${cursor}1.6</source>
<target>1.6</target>
</configuration>
Without java8 and without .exe file you can't go ahead with your requirement.
Hope it's useful.
from your error log it says that
[ERROR] Unable to locate the Javac Compiler in: [ERROR] C:\Program Files\Java\jre1.8.0_92\..\lib\tools.jar
paste this code in your pom.xml inside build tag and make sure you add dependencies properly and please change your java version accordingly
this will only work if you have something wrong in your pom.xml.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_HOME}/bin/javac</executable>
<compilerVersion>1.8</compilerVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<jvm>${JAVA_HOME}/bin/java</jvm>
<forkMode>once</forkMode>
</configuration>
</plugin>
</plugins>
note your JAVA_HOME must point like this
C:\Program Files\Java\jdk1.8.0_65
edit
There must be various reasons like
your pom is not getting path to java home
your java home is not set
you are using jre instead of jdk in your IDE
You can download the version of the JDK without installer.
Then specify a JDK 1.8 as source and target in the compiler configuration of the pom.xml with which you have your problem. in this way you don't need changing your JAVA_HOME :
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javac</executable>
</configuration>
</plugin>
<build>
I followed steps in the below link to get the portable JDK:
http://bgasparotto.com/convert-jdk-exe-zip/
I'm on Mac OS X 10.9.2 and sbt 0.13.3-SNAPSHOT (built from the sources), Java 8 and sbt-proguard 0.2.2 plugin.
sbt 0.13.3-SNAPSHOT
[jacek]> sbtVersion
[info] 0.13.3-SNAPSHOT
Java 8
$ /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -version
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
project/plugins.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-proguard" % "0.2.2")
When I ran proguard:proguard in sbt shell it blew up with the following exception:
[sbt-updates]> show proguard:proguard
[info] ProGuard, version 4.9
[info] Reading program directory [/Users/jacek/oss/sbt-updates/target/scala-2.10/sbt-0.13/classes] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-concurrent_2.10/bundles/scalaz-concurrent_2.10-7.1.0-M6.jar] (filtered)
[info] Reading program jar [/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-core_2.10/bundles/scalaz-core_2.10-7.1.0-M6.jar] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-effect_2.10/bundles/scalaz-effect_2.10-7.1.0-M6.jar] (filtered)
[info] Reading library jar [/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar]
[error] Error: Can't read [/Library/Java/JavaVirtualMachines/jdk1.8.0.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)))
[trace] Stack trace suppressed: run last proguard:proguard for the full output.
[error] (proguard:proguard) Proguard failed with exit code [1]
[error] Total time: 16 s, completed Apr 19, 2014 2:27:56 PM
Why could be the reason for the error?
It appears that ProGuard and hence sbt-proguard don't support Java 8 yet and changing the version of Java used in the script to launch sbt helped.
[sbt-updates]> show proguard:proguard
[info] Compiling 8 Scala sources to /Users/jacek/oss/sbt-updates/target/scala-2.10/sbt-0.13/classes...
[warn] there were 6 feature warning(s); re-run with -feature for details
[warn] one warning found
[info] ProGuard, version 4.9
[info] Reading program directory [/Users/jacek/oss/sbt-updates/target/scala-2.10/sbt-0.13/classes] (filtered)
[info] Reading program jar [/Users/jacek/.ivy2/cache/org.scalaz/scalaz-concurrent_2.10/bundles/scalaz-concurrent_2.10-7.1.0-M6.jar] (filtered)
...
This is with the following version of Java 7:
$ /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
The question is really interesting and i was facing the same problem with the only difference that i added Proguard programmatically by using maven. Consequently, i considered that it will be helpful to post my solution although it is a bit different from the main question. For all those that using maven and faces the same problem, my workaround fix was to update the version of Proguard by using it as runtime inside the plugin, so the working pom.xml looks like this
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.14</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<attach>true</attach>
<appendClassifier>false</appendClassifier>
<addMavenDescriptor>true</addMavenDescriptor>
<injar>${project.build.finalName}-jar-with-dependencies.jar</injar>
<injarNotExistsSkip>true</injarNotExistsSkip>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
<lib>${java.home}/lib/ext/sunjce_provider.jar</lib>
</libs>
<options>
<option>-allowaccessmodification</option>
<option>-optimizationpasses 3</option>
<option>-overloadaggressively</option>
<option>-repackageclasses ''</option>
<option>-dontusemixedcaseclassnames</option>
<option>-dontskipnonpubliclibraryclasses</option>
<option>-flattenpackagehierarch</option>
<option>-dontwarn</option> <!-- added option to ignore com.sun missing classes -->
<option>-keep public class com.StocksNews.App {
public static void main(java.lang.String[]);
}
</option>
</options>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>6.1.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
It's possible to update the version of Proguard that sbt-proguard uses by changing the key proguardVersion in build.sbt to a setting newer than 5.0, e.g.
ProguardKeys.proguardVersion in Proguard := "5.2.1"
See: https://github.com/sbt/sbt-proguard/issues/5
I fixed that by update to latest version of ProGuad from link below
Sourceforge ProGuard page
Add classpath "com.guardsquare:proguard-gradle:7.0.1" in the project level build.gradle to resolve the error. I have solved this issue by adding the same.
I will recommend you to upgrade the Gradle Version to 7.x.x, which will remove ProGuard and you have to migrate to R8, as R8 is backward compatible with your exisiting code ProGuard rules. For Migration please read docs other you will waste alot time in figuring out some feature is not working after migration same happened to me.