1

I'm trying to create a .bat file to run my generated executable JAR file. I found this method of creating .bat files for running a project. So, I read up on the plugin here and added the following to my pom.xml.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>assemble</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <assembleDirectory>${assembleDir}</assembleDirectory>
        <generateRepository>false</generateRepository>
        <repositoryName>lib</repositoryName>
        <configurationDirectory>conf</configurationDirectory>
        <copyConfigurationDirectory>false</copyConfigurationDirectory>
        <programs>
            <program>
                <mainClass>com.companyname.tests.TestRunner</mainClass>
                <id>AutoConfigTest</id>
            </program>
        </programs>
    </configuration>
</plugin>

And, yes, as the name suggests, this JAR contains JUnit test cases.

I prevented the plugin from unpacking JARs and creating the repo folder and set that to my already generated lib folder, which contains all the JARs(executables and the dependencies). The .bat file is being generated but, when running it, I'm getting the following error.

Error: Could not find or load main class com.companyname.tests.TestRunner

Also, I want the command prompt to stay after execution. In this case it is closing immediately. Maybe its because I'm getting an error. I'm not sure.

So, got into searching again and found this. But as the accepted answer suggests, my pom.xml already contains -

<packaging>jar</packaging>

The assembled directory is -

AutoConfigTest
 |
 |--bin
 |   `- contains the .bat file
 |--conf
 |   `- contains the property files and other configuration files
 |--lib
     `- contains all the JARs

What am I doing wrong here?

Community
  • 1
  • 1
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
  • Yes, that [question you mention](http://stackoverflow.com/q/15228501/6042824) looks exactly like yours. The accepted solution is strange, as `jar` is default packaging. Did you try the other solutions and suggestions? It seems there are several things that can go wrong and each other answer tackles one of those things. – Anton Koscejev May 30 '16 at 07:32
  • What is the content of the generated `.bat` file? Does any `.jar` file in `lib/` contain your class `com.companyname.tests.TestRunner`? – SubOptimal May 30 '16 at 08:02
  • @SubOptimal Yes, the jar which contains `com.companyname.tests.TestRunner` in present in the `lib` folder. I've checked it. – Bilesh Ganguly May 30 '16 at 09:43
  • @BileshGanguly I assume it is not at the place the script `AutoConfigTest.bat` expect it. Have a look at my answer. – SubOptimal May 30 '16 at 10:01
  • @SubOptimal I've already added the dependencies to the classpath. Please take a look at the generated batch file. I've updated the question. – Bilesh Ganguly May 30 '16 at 10:07
  • What does `jar tf target\appassembler\lib\AutomatedConfigurationTestingTests-1.jar | findstr TestRunner.class` return? – SubOptimal May 30 '16 at 10:18
  • @SubOptimal The main class isn't in `AutomatedConfigurationTestingTests-1.jar`. That only I mentioned in the comment on your answer. You are correct in pointing out that the main class isn't visible to the batch file. – Bilesh Ganguly May 30 '16 at 10:22

2 Answers2

1

Maybe it's related to (from the README.md)

All dependencies and the artifact of the project itself are placed in a generated Maven repository in a defined assemble directory. All artifacts (dependencies + the artifact from the project) are added to the classpath in the generated bin scripts.

In your pom.xml you prevent the generation of that repository. So you need to ensure that the artifact from the project is copied at the expected place.

Assuming following project settings

<groupId>com.companyname</groupId>
<artifactId>Maven-AppAssembler</artifactId>
<version>0.0.1-SNAPSHOT</version>

the artifact is expected to be at (the CLASSPATH setting in the scripts bin/AutoConfigTest)

"$REPO"/com/companyname/Maven-AppAssembler/0.0.1-SNAPSHOT/Maven-AppAssembler-0.0.1-SNAPSHOT.jar

where $REPO resolve to target/appassembler/lib.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • I think I found the error. You are correct in pointing out that the main class isn't visible to the batch file. For some reason, the test jar file (which contains the main class) isn't being added to the classpath variable of the batch file. – Bilesh Ganguly May 30 '16 at 10:17
0

I found the issue. @SubOptimal was correct in pointing out that the main class isn't visible to the batch file.

For some reason, the test JAR file (which contains the main class) isn't being added to the classpath variable of the batch file. As a result, whenever I ran the batch file, I was getting the error mentioned in the question.

I went back to the documentation and found this.

Sometimes it happens that you have many dependencies which means having a very long classpath, and becomes too long (in particular on Windows based platforms). This option can help in such situation. If you activate this option, your classpath contains only a classpath wildcard (REPO/*). But be aware that this works only in combination with Java 1.6 and above and with repositoryLayout flat.

So, instead of adding individual JAR files into the path, I added the whole lib directory to the classpath by adding the following to the pom.xml.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.10</version>
    ...
    <configuration>
        ...
        <repositoryLayout>flat</repositoryLayout>
        <useWildcardClassPath>true</useWildcardClassPath>
        ...
    </configuration>
    ...
</plugin>

I could do this because the repository layout of lib was already flat. There were no hierarchies. No other change was required. The batch file now behaves as expected.

Community
  • 1
  • 1
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58