0

I have Maven 2.2.1, which uses JAVA 1.6. But I need to compile and execute my project with 1.7. I don't want to change JAVA_HOME variable because of other projects, bur as far I know, I can configurate that in pom.

With the code below, I can compile my project, but I cannot execute it because of minor version. What do I do wrong? Or is it impossible to do it all in pom? Or does it have something to do with maven version (2.2.1).

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
             <source>1.7</source>
             <target>1.7</target>
             <fork>true</fork>
             <executable>...path-to-my-1.7-javac...</executable>
             <compilerVersion>1.7</compilerVersion>
             <encoding>UTF-8</encoding>
         </configuration>
 </plugin>

EDIT: SOLUTION WITH MAVEN-EXEC-PLUGIN

Problem solved with adding the configuration below:

 <properties>
      <arg0>defaultParam1</arg0>
 </properties>


 <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>exec-maven-plugin</artifactId>
        <executions>
           <execution>
             <goals>
                <goal>exec</goal>
             </goals>
           </execution>
         </executions>
        <configuration>
          <executable>path-to-java-1.7/bin/java</executable>
          <arguments>
            <argument>-classpath</argument>
             <classpath/>
             <argument>${main.class}</argument>
             <argument>${arg0}</argument>
      </arguments>
        </configuration>
    </plugin>
Salvadora
  • 359
  • 1
  • 3
  • 11

1 Answers1

0

Take a look at How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version Install JRE 7 and you are good (maven is a build tool, not a run time tool)

Community
  • 1
  • 1
Shmil The Cat
  • 4,548
  • 2
  • 28
  • 37
  • I have java 7 installed, like I said in my question. It would work, if java_home would point to java 1.7., but I don't want to change it. I want to enforce usage of 1.7. for this particular project in pom. And of course, you can run java code from maven. I tried to execute the code with exec-plugin and with builded jar. It does work with JAV_HOME = 1.7. – Salvadora Apr 08 '13 at 17:56
  • @Salvadora install it w/o changing JAVA_HOME ... say you out JRE7 in c:\temp\Foo all you need to do is to call c:\temp\Foo\java -jar \builder.jar – Shmil The Cat Apr 08 '13 at 17:59
  • Shmil, thanks for your replay. I know taht I can exeute my code, if I give the path to jre in the terminal. But that is what I want to avoid. Maybe it is not possible to do it when I am using jar, but at least when I am using exec:java (maven-exec-plugin). Can I configurate exec-plugin to do so? – Salvadora Apr 08 '13 at 18:47
  • I configurated exec-plugin (see EDIT in my question). But ist still doesnt't work to run the code with 1.7. with mvn exec:java – Salvadora Apr 08 '13 at 22:52
  • Problem partly solved: I configurated plugin with goal exec and now I can run with 1.7. Just one problem left: How do I pass -Dexec.args as argument to exec, so I can pass args to main method? – Salvadora Apr 09 '13 at 21:20