SandTh
SandTh

Reputation: 2399

Running java from within the console in eclipse with maven

So, this isn't about running java code from within eclipse but executing the java command from maven inside eclipse (outside eclipse it works just fine).

To be concrete:

we have a .cmd file we execute from within maven, the relevant parts of it:

java -classpath (the classpath) com.bea.wls.ejbgen.EJBGen (and some other stuff)

When run from the command line (mvn clean install), we do get this output:

EJBGen WebLogic Server 12.1.3.0.0  Wed May 21 18:53:34 PDT 2014 1604337

 Creating my\package\ejb\Local.java
 Creating my\package\ejb\WebserviceLocal.java
 Creating my\package\ejb\ServiceLocal.java
 Creating my\package\ejb\LocalHome.java
 Creating my\package\ejb\WebserviceLocalHome.java
 Creating my\package\ejb\ServiceLocalHome.java

all is well,

however when executed from within eclipse:

EJBGen isn't supported by JDK version higher than 1.7

Now, how does the console/m2e run chooses which java binary to use?

installed jre's

run configuration

Any help/pointers greatly appreciated!!!

S.

edit:

the configuration of (indeed) the exec-maven-plugin plugin:

        <plugin>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <groupId>org.codehaus.mojo</groupId>
            <executions>
                <execution>
                    <id>runEJBGen</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>runEJBGen.cmd</executable>
                <!-- optional -->
                <workingDirectory>${project.basedir}\src\main\java</workingDirectory>
            </configuration>
        </plugin>

we use this mechanism (and not the java variant) because of other - project specific - reasons...

edit 2: compiler options of pom.xml

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

Upvotes: 3

Views: 1026

Answers (1)

Pierre B.
Pierre B.

Reputation: 12943

Basically you are trying to run java -classpath ... command from within another Java process, and thus creating a new native process. From this point, whatever the configuration you have for Maven, Eclipse, etc. are ignored and only the PATH environment variable set for the calling process will decide what Java version will be run, i.e. the first java executable found on your PATH will be used.

Your processes looks like this:

|- root process
  |- Eclipse process (run on Java 8 as per Eclipse config)
    |- Maven process (run on Java 7 as per M2E run config)
      |- New native process which will execute your .cmd file
        |- New Java process running from first java executable found on PATH entries

Each of these processes inherit the PATH of its parent and can manipulate it. Eclipse is probably adding new entries on your PATH containing Java 8 executables. For example, when I run a Maven build from within my Eclipse and print the PATH variable to compare it with the same build outside Eclipse, the Eclipse process have the following entries at the beginning which are not present outside Eclipse:

C:/Program Files/Java/jre1.8.0_121/bin/server
C:/Program Files/Java/jre1.8.0_121/bin
C:/Program Files/Java/jre1.8.0_121/lib/amd64

Meaning that my Eclipse process added Java 8 entries to my PATH. I cannot find any related documentation concerning these entries, but chances are your issue is the same. You can try to echo the PATH variable using something like System.out.println(System.getenv("PATH")); before executing your .cmd file to compare it between Eclipse and outside Eclipse.

Try to make sure your script runs the proper Java version. You have various options:

  • (EDIT:) as suggested by @Bamboomy, using %JAVA_HOME%/bin/java in the script to use the Java executable pointed by JAVA_HOME environment variable
  • Instead of running java which will search for an executable on your PATH, use a complete path instead such as C:/java7/bin/java.exe
  • Configure your script to search for a java executable on your PATH, test the version and only run the java executable that matches a version you expected (you can use the where command on Windows)
  • Manipulate the calling process PATH environment variable before executing your script to ensure a proper Java version is used
  • Any other way ensuring that a proper java executable is run by your script ;)

Note: eventually the best way to go would be to avoid running a process-calling-a-process-calling-a-process, its bound to cause errors like these.

Upvotes: 3

Related Questions