albfan / mvnexec

bash script to find and execute java classes with main methods
18 stars 12 forks source link

Unable to run a class with main method #3

Closed YounesRahimi closed 8 years ago

YounesRahimi commented 8 years ago
executing ir.iais.activemqtut.salary.ActiveMQMailApp
/Users/yoones/bin/mvnexec: process substitution: line 95: syntax error near unexpected token `&'
/Users/yoones/bin/mvnexec: process substitution: line 95: `$JAVA -version |& awk -F '"' '/version/ {print $2}''
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ActiveMQ-Tutorial 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default-cli) @ ActiveMQ-Tutorial ---
Error: Could not find or load main class ir.iais.activemqtut.salary.ActiveMQMailApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.458 s
[INFO] Finished at: 2016-08-06T16:54:35+04:30
[INFO] Final Memory: 9M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2:exec (default-cli) on project ActiveMQ-Tutorial: Command execution failed. Process exited with an error: 1(Exit value: 1) -> [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/MojoExecutionException

The error lines are these;

...
/Users/yoones/bin/mvnexec: process substitution: line 95: syntax error near unexpected token `&'
/Users/yoones/bin/mvnexec: process substitution: line 95: `$JAVA -version |& awk -F '"' '/version/
...

My class is:

package ir.iais.activemq.salary;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Hello world!
 */
public class ActiveMQMailApp {
    private static volatile int counter = 0;

    public static void main(String[] args) throws Exception {
        MailConsumer.initService();
        for (int i =0;i<11;i++) MailProducer.sendMail(fakeMail());
        System.out.println("counter is: " + counter++);
    }

    private static String fakeMail() throws InterruptedException {
        Thread.sleep(1000);
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd  hh:mm:ss");
        return "<html><body><p>Date: <b> "+sdf.format(new Date())+" </b></p></body></html>";
    }

}

I also commented all body of main method but it still throw the same error.

ActiveMQMailApp class run with this command correctly:

 mvn exec:java -Dexec.mainClass="ir.iais.activemq.salary.ActiveMQMailApp"
albfan commented 8 years ago

Let me know your environment

|& is a bashism to redirect both stderr and stdout. Try it without &

If that works maybe we can find a posix solution or add both redirections as an feature option (redirect-both) or global option (no-posix)

albfan commented 8 years ago

Check this:

http://stackoverflow.com/a/16497456/848072

replace |& with 2>&1 | and it should work.

albfan commented 8 years ago

That was added for detect old java version, so I guess is fair to detect it with bash<4. |& was introduced on bash 4.

Feel free to reopen if it keeps failing, Thanks.

YounesRahimi commented 8 years ago

Hi @albfan. The first bug fixed, but the class cannot run yet. the error is this:

executing ir.iais.activemqtut.salary.ActiveMQMailApp
mvn exec:exec -Dexec.executable=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin/java -Dexec.workingdir=. -Dexec.args=-cp target/classes:target/test-classes:target/dependency/activemq-all-5.13.3.jar:target/dependency/hamcrest-core-1.3.jar:target/dependency/junit-4.12.jar:target/dependency/log4j-1.2.17.jar:target/dependency/slf4j-api-1.7.7.jar:target/dependency/slf4j-log4j12-1.7.12.jar ir.iais.activemqtut.salary.ActiveMQMailApp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ActiveMQ-Tutorial 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default-cli) @ ActiveMQ-Tutorial ---
Error: Could not find or load main class ir.iais.activemqtut.salary.ActiveMQMailApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.448 s
[INFO] Finished at: 2016-08-10T11:58:21+04:30
[INFO] Final Memory: 9M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2:exec (default-cli) on project ActiveMQ-Tutorial: Command execution failed. Process exited with an error: 1(Exit value: 1) -> [Help 1]

I added a line to mvnexec file to print out the maven command on line 2. I'm on Mac OS X 10.11.6 with jdk1.8.0_65 using Oh My ZSH in iTerm2 v3!

While running ActiveMQMailApp class has problem, another class in this project ( ir.iais.activemqtut.App) can be run nicely.

▶ mvnexec
1) ir.iais.activemqtut.App
2) ir.iais.activemqtut.salary.ActiveMQMailApp
#?

Body of ActiveMQMailApp is as same as before.

albfan commented 8 years ago

I bet MailConsumer.initService(); is crashing in some way. See it exits with a 1 value.

Maybe a try/catch on main method with a printStacktrace() would give you a hint of the problem.

If your code is avaliable would be interesting to see if there's something about plugin exec version configurable, because there's a problem with output flush prior to version 1.3.2 and you (and everybody I guess) use 1.2 version through mvnexec.

https://github.com/mojohaus/exec-maven-plugin/commit/23fe65fbf5bf2b4ffe241d30b754342a2674c80c