0

I'm attempting to use the socket.io implementation for Java. I need 2 dependencies: 1) Engine.IO Java Server and 2) Socket.IO Server Java. I'm struggling to import the actual Java packages once the dependencies have been added to my pom.xml. Looking at the javadocs for Engine.IO Java Server, it looks like the package name to import should be io.socket.engineio.server. However, this results in an error (details below). For other dependencies that I add, what's the best way to find the package name to actually import the dependency in my Java source code?

I'm not using an IDE like IntelliJ/Eclipse. I'm just using a more simple text editor (VSCode) and a terminal. Also, I've ensured the dependencies are actually downloaded and installed in my ~/.m2/repository directory. Here are my files/terminal output.

Pom.xml

<?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.mycompany.app</groupId>
  <artifactId>game-server</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>game-server</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>io.socket</groupId>
      <artifactId>engine.io-server</artifactId>
      <version>6.1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.socket</groupId>
        <artifactId>socket.io-server</artifactId>
        <version>4.0.1</version>
        <scope>compile</scope>
      </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.mycompany.app.App</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

App.java

package com.mycompany.app;

import io.socket.engineio.server;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }

}

Terminal Output

$ mvn compile
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.mycompany.app:game-server >--------------------
[INFO] Building game-server 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ game-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/josh/Repos/game/game-server/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ game-server ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/josh/Repos/game/game-server/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/josh/Repos/game/game-server/src/main/java/com/mycompany/app/App.java:[3,26] package io.socket.engineio does not exis
  • Before you randomly, try with import statements, you can just locate the jar in your .m2, open the jar with winzip or 7zip and find out manually. – Prabhakaran Oct 14 '22 at 14:14
  • @Prabhakaran , I unzipped the .jar. The MANIFEST.MF didn’t provide any useful information. The file path to get to the .class files was io/socket/engineio/server, which matches the package import I used. – Josh Majors Oct 14 '22 at 19:31
  • Things like this is why IDEs exist. Consider adding java support so VSCode – Thorbjørn Ravn Andersen Oct 14 '22 at 20:18

0 Answers0