2

I have a simple java maven project whose pom.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>poc</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <solidity.version>0.7.4</solidity.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.8.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.web3j</groupId>
                <artifactId>web3j-maven-plugin</artifactId>
                <version>4.6.5</version>
                <configuration>
                    <soliditySourceFiles/>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

As you can see I am using web3j library to interact with my blockchain and web3j maven plugin to convert my solidity files to java files.

When I run mvn web3j:generate-sources, it successfully runs and converts the solidity files to java files. But when I run the main method of the project from the IDE, it fails with this error: enter image description here

From the error message above, I tried removing the constructor from my solidity file and the pragma line, and then my project builds and runs fine as expected. So, I am thinking my java project is picking the wrong solidity version somehow.

Any tips/hints as to what may be going on here or how to fix this?

1 Answer 1

2

You can specify solidity version for the web3j-maven-plugin via setting the following property: solidity.version.

In your case, it is already specified as your pom file contains:

   <properties>
         ...
        <solidity.version>0.7.4</solidity.version>
    </properties>

So, the plugin will automatically install the right solc version and compile your contracts using it.

Similarily, for the web3j-gradle-plugin

You can specify the version via adding:

solidity {
      version = "0.7.4"
}

to your build.gradle.

4
  • 1
    isn't the plugin taking the solidity version from the smart contract itself? If yes, which one holds precedence over the other?
    – Ram Patra
    Commented Apr 1, 2021 at 9:59
  • 2
    Exactly, the plugin takes the version automatically from the smart contracts. However, if you want to force a certain version, you can specify it using that property. So the property takes precedence over the version in the smart contract Commented Apr 2, 2021 at 10:12
  • 1
    Thanks for the clarification. However, this error (shown in the image) was due to Intellij-Solidity plugin and not because of web3j nor solidity. I have removed the plugin from my IDE and it works fine. For some reason, the plugin was validating against an older solidity version.
    – Ram Patra
    Commented Apr 2, 2021 at 11:51
  • 1
    I don't see a link between the two. The plugin is independent unless it is setting some properties somewhere. I am using the Intellij-Solidity plugin also but everything works fine. Commented Apr 2, 2021 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.