1

I am new to Maven and I am trying to create a JAR out of a HelloWorld program.

I have done the classpath setting:

enter image description here

This is the pom.xml file:

<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>com.demo</groupId>
  <artifactId>custom-project</artifactId>
  <packaging>maven-plugin</packaging>
  <version>1.0</version>
  <name>custom-project Maven Mojo</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

When I trigger mvn package command, I am getting this error:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.700 s
[INFO] Finished at: 2018-04-13T12:25:12+05:30
[INFO] Final Memory: 9M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project custom-project: Compilation f
ailure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

JAVA_HOME is already set as C:\Program Files\Java\jdk1.8.0_121\bin in classpath. Please suggest how to resolve this issue.

user182944
  • 7,897
  • 33
  • 108
  • 174
  • Possible duplicate of [No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?](https://stackoverflow.com/questions/19655184/no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-ra) – Dhaval Simaria Apr 13 '18 at 08:16

3 Answers3

3

Your JAVA_HOME is set to a JRE not to a JDK. The JRE does not include the Java Compiler, so set JAVA_HOME to a JDK and it should work

Jens
  • 67,715
  • 15
  • 98
  • 113
  • 1
    `JAVA_HOME` is already set to `C:\Program Files\Java\jdk1.8.0_121\bin` and I can validate it from `cmd` using `javac --version`. Please advice how to fix this issue. – user182944 Apr 13 '18 at 08:00
  • @user182944 Check your Windows Path Variable for JRE-related entries. It might overrides your %JAVA_HOME%\bin in your %PATH% variable. – rzo1 Apr 13 '18 at 08:04
  • @user182944 As you can see in your screenshot it is pointed a jre, `javac --version` will use the entry in the "PATH" – Jens Apr 13 '18 at 08:12
  • I changed `JAVA_HOME` to `C:\Program Files\Java\jdk1.8.0_121` and in `Path` I added `%JAVA_HOME\bin`. Now I am able to create the `JAR` using `mvn package` but when I run the JAR using `java -jar custom-project-1.0.jar` I am getting this error: `no main manifest attribute, in target\custom-project-1.0.jar`. Please help. – user182944 Apr 13 '18 at 08:14
  • @user182944 Read about maven-shade-plugin – Jens Apr 13 '18 at 08:45
0

Also you can goto your project properties-> Libraries-> add Library -> JRE system Library -> Alternate JRE -> Add. and then you choose your JDK path from your system and apply. Make sure to remove JRE From project library.

In my case, this works well.

-1

You need to tell java what class to run. Depending on the maven plugin you use the ways might be different. This link shows how it can be done with the help of the maven-assembly-plugin https://stackoverflow.com/a/45902851/6825678

DrHopfen
  • 752
  • 3
  • 13