javahyperledger-fabrichyperledger-chaincode

Cannot istantiate chaincode docker container due to missing Main method in contract class


The chaincode container cannot start (after committing to the channel) due to the following error:

Error: Main method not found in class mypackage.MyChaincodeClass, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

I'm working with fabric-java-chaincode 2.2.3 on a 2.2.2 network.

So the problem seems self explanatory but I'm using the example on Maven java chaincode on GitHub as a guideline and I adapted the maven shade plugin configuration:

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>chaincode</finalName>
                            <transformers>
                                <transformer                                    
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>mypackage.MyChaincodeClass</mainClass>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>                                    
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

the tree of my project is the following:

.
└── project-repo/
    ├── chaincode/
    │   ├── src/
    │   │   └── main/
    │   │       └── java/
    │   │           └── mypackage/
    │   │               └── MyChaincodeClass.java
    │   └── pom.xml
    └── another-module/
        └── ...

I tried to start the uber-jar using java -jar chaincode.jar and I get the same error as the one printed in the container logs.

The manifest in the generate uber-jar looks good to me:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.6.3
Built-By: ultracode
Build-Jdk: 11.0.9
Main-Class: mypackage.MyChaincodeClass

The chaincode uses only the @Contract and @Transaction annotation and implements the ContractInterface interface (I'm not using the @Default annotation beacause this chaincode is not the default one in the channel)


Solution

  • The maven-shade-plugin transformer configuration the mainClass tag must be set like this :

    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
       <mainClass>org.hyperledger.fabric.contract.ContractRouter</mainClass>
    </transformer>
    

    I was missled by the Maven example for the 2.2 release in which the maven shade plugin transformer is set like this:

    
    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
       <mainClass>chaincode.example.SimpleChaincode</mainClass>
    </transformer>
    
    

    In later versions the error was fixed, also the Gradle example in the same release has the correct setting.