Search code examples
javamavenintellij-ideaipfs

How to create a remote connection in ipfs using maven in Intellij Idea?


I am new to this IPFS thing and I am really desperate to ask this, I really have no idea how am I going to implement IPFS in my machine, do I need to install something, or where specifially should I run these commands?

IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001");
NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(new File("hello.txt"));
MerkleNode addResult = ipfs.add(file).get(0);

Solution

  • Assuming you want to use java-ipfs-api You're going to need to add the following to your pom.xml file.

      <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
      </repositories>
    
      <dependencies>
        <dependency>
          <groupId>com.github.ipfs</groupId>
          <artifactId>java-ipfs-api</artifactId>
          <version>v1.2.0</version>
        </dependency>
      </dependencies>
    

    This will download and setup the library for you to write code with. Your code above should work. Here is a complete example if needed

    import io.ipfs.api.IPFS;
    import io.ipfs.api.MerkleNode;
    import io.ipfs.api.NamedStreamable;
    
    import java.io.File;
    import java.io.IOException;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/5001");
    
            NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(new File("hello.txt"));
    
            MerkleNode addResult = ipfs.add(file).get(0);
        }
    
    }