Home > Blockchain >  VSCode: how to share .java files between projects
VSCode: how to share .java files between projects

Time:01-04

I am fairly new to Java so might be missing the obvious here.

I want to pull .java files (not jars) from a folder that is "external" to the Project. I am developing the MainProject and the Shared java files concurrently. Later I will want to use the Shared files in another Project (most likely also as .java files since that other Project will be related to the first - i.e. more concurrent development. ;-)

I can't work out how to use a .java file from the SharedJava folder in the MainProject. I have tried what is suggested in SO etc to no avail.

I have set up a new project with folders like so:

enter image description here

I have created VSCode Java Projects in both folders, although for the SharedJava I didn't include any build tools. Maybe that is an issue?

Both projects appear in the JAVA PROJECTS section in VSCode.

Expanded, it looks like below, and as an initial test the highlighted java file is the one I would like to import into the MainProject.

enter image description here

The SharedClass.java file:

package messaging;

public class SharedClass {
  public void echoMessage(String msg) {
    System.out.println("SharedClass.echoMessage() said: "   msg);
  }
}

and what I am trying to do for my test is in this MainProject App.java file:

package com.flowt;

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

        // How to import SharedClass ?
        SharedClass sharedClass = new SharedClass();
        sharedClass.echoMessage("Hello XYZ");
    }
}

My MainProject.code-workspace file is this:

{
    "folders": [
        {
            "path": "."
        },
        {
            "path": "../SharedJava/flowtshared"
        }
    ],
    "settings": {
        "java.project.sourcePaths": [
            "src",
            "../SharedJava/flowtshared/src"
        ]
    }
}

So, what do I need to do to be able to import SharedClass.java into MainProject? (without creating a symlink!)

Thanks, Murray

CodePudding user response:

Ok. Got it working.

Firstly: The best explanation I found for understanding Maven is here: enter image description here

The parent pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>

  <name>The Parent project for my test multi modules</name>

  <groupId>com.flowt.multitest</groupId>
  <artifactId>parent</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <packaging>pom</packaging>

  <modules>
    <module>shared</module>
    <module>mainapp</module>
  </modules>

  <dependencies>

    <!-- The rest of this file is as normal ... -->

The shared 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>

  <!-- Refer to the parent pom.xml -->
  <parent>
    <groupId>com.flowt.multitest</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>

  <!--
    Since we have a parent we leave this out
    because it is pulled from the parent pom.xml

    <groupId>com.flowt.shared</groupId>
  -->
  <artifactId>shared</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <!-- The rest of this file is as normal ... -->

The mainapp 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>

  <!-- Refer to the parent pom.xml -->
  <parent>
    <groupId>com.flowt.multitest</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>

  <artifactId>mainapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <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>
    <!-- Here you must include the shared project as a dependency -->
    <dependency>
      <groupId>com.flowt.multitest</groupId>
      <artifactId>shared</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

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

  </dependencies>

  <!-- The rest of this file is as normal ... -->

PrintMessage.java

package com.flowt.messaging;

public class PrintMessage {
  public String echoMessage(String msg) {
    return "PrintMessage.echoMessage() echoed: "   msg;
  }
}

App.java


package com.flowt.app;

// This class is in the shared project: shared/src/main/java/com/flowt/messaging/PrintMessage.java
import com.flowt.messaging.PrintMessage;

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

        // The shared class
        PrintMessage printMessage= new PrintMessage();
        String echo = printMessage.echoMessage("I was called from Main app");
        System.out.println(echo);
    }
}

I hope that helps and any expert advice on structuring an app is most welcome.

Go well, Murray

  • Related