how to import github or other internet code (project) into java project

40 views Asked by At

I need to put some variable code from internet into my project. Any libraryes, plugins, etc if needed (I use gradle and parse/parse4j). I am newbie in java, but have some skills with javascript, so i know how code works. So this is how it shoud like: i have my varible project at github.com/account/rep or something like that, than i need to put that stuff into my general project. I have project/generalproject.java and project/varible.java project. That shoud be command in varible project like ' command: 'url'; ' or something like that. enter image description here

1

There are 1 answers

0
dan1st might be happy again On

Java doesn't really (if we ignore remote classloading) have a way to import code from somewhere else. It doesn't allow you to just import code from GitHub.

Pure Java way

Instead, you would first download a JAR (you could also have a repository with compiled class files) of the library you want. You can then add to to your classpath by adding --class-path to the command-line of both java and javac.

If we assume you have a JAR called myjar.jar with a class Util in the package somelibrarypackage, you can import that package like any other package using import somelibrarypackage.Util;.
For this to compile, you would need to add --class-path myjar.jar to javac, for example

# this assumes MainClass is in the default package
javac --class-path myjar.jar MainClass.java
java --class-path myjar.jar MainClass

Technically, there's also the module path if you want to use modules/have a module-info.java. In that case, you would use --module-path instead of --class-path.

Gradle

Build tools actually do that part for you. For this, you would first need the library/project you are using to be deployed on Maven Central (or a different Maven repository). Typically, libraries have done that already and include information about their "artifact coordinates" in the README.

Artifact coordinates identify libraries on Maven Central. For each library published, there should be a unique group id and artifact id. Aside from that, there will be a version identifying the version of the library you are using.
For example, JUnit uses the group ID org.junit.jupiter and the artifact ID junit-platform-api. The latest version of it (at the time of writing this answer) is 5.10.1.

You can also use this Website to search through dependencies on Maven Central and find their artifact coordinates.

Since you are using Gradle, you should have a build.gradle files containing a dependencies block. In there, you can specify which libraries you want to use using artifact coordinates and it should automatically download them and add them to the classpath.
In order to add a dependency to a Gradle project, you add implementation 'ARTIFACT_COORDINATES_HERE' where you replace ARTIFACT_COORDINATES_HERE with the group ID followed by a :, the artifict id, another : and the version. For example, for JUnit, this would be:

implementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'