#How to download libraries?

1 messages · Page 1 of 1 (latest)

floral lantern
#

⚠️ Warning: I'm an an extreme noob to java
I have a couple years under my belt in python so please excuse any incorrect lingo I may use, etc. etc.
working on a college project which requires java, and I used a python to java translator (shamelessely) to convert an old personal project for re-purposing
the program begins with:```java
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

tryna figure out how to get them since I'm using intellij's idea atm, although the program worked flawlesely (without any lib downloads) on repl.it
tried researching and I've gone down some maven repository rabbithole
I have no clue what I'm doing, please help me, I am desperate
thanks in advance 🙏
gaunt ridgeBOT
#

<@&987246452180930620> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

To download libraries in Java, you typically use a build tool like Maven or Gradle. These build tools manage dependencies and automatically download the required libraries for your project.

Here's a step-by-step guide to downloading libraries using Maven:

  1. Install Maven: If you haven't already, download and install Maven from the Apache Maven website (https://maven.apache.org/download.cgi).

  2. Create a new Maven project: Open your command prompt or terminal and navigate to the directory where you want to create your project. Run the following command to create a new Maven project:

#
   mvn archetype:generate -DgroupId=com.example -DartifactId=myproject
#
  1. Navigate to the project directory: Move into the newly created project directory using the following command:
#
   cd myproject
#
  1. Open the pom.xml file: The pom.xml file is where you define your project's dependencies. Open it in a text editor.

  2. Add dependencies: Inside the <dependencies> tag in pom.xml, add the dependencies you want to download. For example, if you want to use the Apache Commons Lang library, add this code:

#
   <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.12.0</version>
   </dependency>
#
  1. Save and close pom.xml.

  2. Download dependencies: Run the following command in your project directory to download all dependencies specified in pom.xml:

#
   mvn dependency:resolve
#
  1. Build your project: Once all dependencies are downloaded, run this command to build your project:
#
   mvn package
#
  1. Libraries are downloaded! The libraries specified in pom.xml will be downloaded into your local repository (usually located at ~/.m2/repository).

That's it! You have successfully downloaded libraries using Maven. You can now use these libraries in your Java project.

Note: The process is similar with Gradle, but instead of a pom.xml file, you'll have a build.gradle file where you define dependencies.

floral lantern
#

wtf

#

that is so cool?!

#

yeah not too sure if that's a useful answer?

#

I'd preferably avoid downloading any more than I already have, is there anywhere I can simply pull the dependencies and throw them in my project?

soft cradle
#

Imports are the same as what you're familiar with in python

#

The answer above is correct, since you're referencing org.json which is an external library you'll need to download it

#

Maven is like pip

lament moat
#

ah pip right

floral lantern
floral lantern
soft cradle
#

There similar in the sense they'll download the libraries for you but there are some differences

#

But know you should use maven to manage your dependencies

#

(or gradle if you want something a bit easier)

floral lantern
#

I mean if maven is the standard, I'll try find my way around it

soft cradle
#

If you're using intellij, when you create a project, during the setup, they'll be options to choose maven or gradle

soft cradle
#

So you should learn both imo if you're investing in Java development

floral lantern
soft cradle
#

Intellij is the best code editor

#

Especially for Java

#

If you use it, it'll handle everything you'd typically need

#

Right out of the box

#

And it has amazing tooling built in

#

It's also a beautiful ide

floral lantern
#

not me using vsc for the past 6 years

#

I appreciate it, it's a great program in all honesty, but just too much for me

soft cradle
#

np

#

If you've been using it for 6 years then you'll be fine in vsc

#

But make sure to download all the right plugins or whatever you vsc guys need to do to make your glorified notepad work

ocean rivet
#

@floral lantern if you're willing, I can get you set up with my tool for this

floral lantern
#

I've never bothered with vsc plugins lol

floral lantern
soft cradle
#

Have you heard the term "bs code" lol

#

A nickname for vsc

floral lantern
#

no but I see where you're coming from 🤣

soft cradle
#

I wouldn't recommend being emcues lab rat pepekek

ocean rivet
#

well, I wrote a tool that gets you dependencies

floral lantern
#

I mean

ocean rivet
#

they always come out better in the end

floral lantern
#

if it doesn't break my barely functioning system

#

and it works

ocean rivet
#

cool

floral lantern
#

I'm willing to be your lab rat lmao

soft cradle
#

That you do, you'll be in capable hands @floral lantern

ocean rivet
#

what OS are you using?

floral lantern
#

windows

#

and no, you can't judge me

soft cradle
#

Windows is the best os dw

floral lantern
#

he gets it

soft cradle
#

I'll fight any Linux or Mac nerd

#

Anyways gl you both

floral lantern
#

java community better than I thought

floral lantern
#

thank you for explaining what maven is, their explanation on the site is so bad 😭

soft cradle
#

"pip"

ocean rivet
#

okay download this

ocean rivet
#

jresolve-windows-latest.exe

floral lantern
#

just got 13 windows defender alerts

#

hyped!

floral lantern
#

only called jresolve.exe tho

ocean rivet
#

thats good

floral lantern
#

the zip was called jresolve-windows-latest.exe.zip

floral lantern
ocean rivet
#

open your terminal

floral lantern
#

yep

floral lantern
#

sorry

ocean rivet
floral lantern
#

that'll be my second choice if this doesn't work (pls work)

ocean rivet
floral lantern
#

right

#

where is this going to be downloaded

hazy oyster
#

-_-

ocean rivet
floral lantern
#
C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar;C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\org\easymock\easymock\2.2\easymock-2.2.jar;C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\jmock\jmock\1.1.0\jmock-1.1.0.jar;C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\junit\junit\4.10\junit-4.10.jar;C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar
ocean rivet
#

yep - so thats all of the dependencies that json-simple wants

#

idk why it wants ...junit? but okay whatever

floral lantern
# floral lantern ``` C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\org\hamcrest\ham...
C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar;
C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\org\easymock\easymock\2.2\easymock-2.2.jar;
C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\jmock\jmock\1.1.0\jmock-1.1.0.jar;
C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\junit\junit\4.10\junit-4.10.jar;
C:\Users\gytis\.jresolve\cache\https\repo1.maven.org\maven2\com\googlecode\json-simple\json-simple\1.1.1\json-simple-1.1.1.jar
ocean rivet
#

so now you can do

floral lantern
ocean rivet
#
jresolve.exe --output-directory libraries pkg:maven/com.googlecode.json-simple/[email protected]
#

and that will put all the dependencies into that folder

#

then you just need to compile with

#
javac --module-path libraries --add-modules ALL-MODULE-PATH  ...
#

and run with

#
java --module-path libraries --add-modules ALL-MODULE-PATH  ...
floral lantern
#

assuming the elipsis is to be replaced with my desired path for all of the libraries?

ocean rivet
#

no that is represented by "Libraries" in this command

#
jresolve.exe --output-directory libraries pkg:maven/com.googlecode.json-simple/[email protected]
floral lantern
#

oh

#

wait so where do I find the directory "libraries"

ocean rivet
#

here

#

its the "maven central" repo

#

there are a few websites to browse it, but this is the "official" one

#

you generally get all your libraries from there

floral lantern
#

right

#

I was looking at mvn repositories

#

I think

ocean rivet
floral lantern
#

couldn't find no download button on there tho 😭

ocean rivet
#

so you copy that pkg:maven stuff from there

floral lantern
#

I think I'm following?

#

hold on

#

I've run the first two commands

#

is the third one java or javac

ocean rivet
#

well okay

#

don't do the java or javac ones yet

#

first, the libraries were downloaded to the "libraries" folder

floral lantern
#

oh?

ocean rivet
#

which isn't a global thing

floral lantern
ocean rivet
#

so you might want to run that command in the directory your project is

floral lantern
#

oh

#

wait

ocean rivet
#

just type dir or cd libraries and you'll see

floral lantern
#

found it

#

C:\Users\gytis\libraries 🤦‍♂️

#

where should I move that folder

ocean rivet
#

right next to your code

#

so if you have

#
src/
   Some.java
#

put it

#
src/
    Some.java
libraries/
    ...
floral lantern
#

I got a weird setup

#

I have one repo with a ton of repos inside of it

#

if that makes sense

#

so can I throw libraries in this specific project's sub-folder?

ocean rivet
#

yes

floral lantern
#

ok cool

ocean rivet
#

now, idk how VSCode is gonna react (I actually haven't used it with Java much)

#

but that is the right place

floral lantern
#

I'm not using vsc rn

#

got intellij for java

ocean rivet
#

oh good

floral lantern
#

anyways

ocean rivet
#

then once you see the libraries folder in IntelliJ

floral lantern
#

set my terminal's dir to the project's path

ocean rivet
#

right click it and click "add as library"

ocean rivet
#

and just download it there

floral lantern
#

java or javac

ocean rivet
#

neither

#
jresolve.exe --output-directory libraries pkg:maven/com.googlecode.json-simple/[email protected]
#

this one

floral lantern
#

oh

ocean rivet
#

that downloads the library and all its transitive deps to that folder

floral lantern
#

alr I ran it

ocean rivet
#

okay now in IntelliJ you should eventually see the libraries folder

floral lantern
#

yep

ocean rivet
#

might need to restart or refresh but idk

floral lantern
#

nah it's working

hazy oyster
#

why going in such lengths when you have maven

ocean rivet
#

then right click, add as library

ocean rivet
floral lantern
#

project global local?

ocean rivet
floral lantern
#

okok

#

yay

#

all the 115 red lines are gone

ocean rivet
#

Now one more improvement

floral lantern
#

can't wait to run my code and it doesn't work

ocean rivet
#

make a new file named deps

soft cradle
#

(I've got a little interest in emcues project so I'm just spectating the setup)

ocean rivet
#

short for dependencies

floral lantern
ocean rivet
#

yeah

#

it actually doesn't matter what you call it

#

it can be whatever.txt if you want

floral lantern
#

done

ocean rivet
#

okay now paste this in as the first line

floral lantern
#

sure

ocean rivet
#

and heck, put this as the 2nd line

#
pkg:maven/org.json/json@20231013
#

lets get you two json libraries

floral lantern
#

funny words

ocean rivet
#

now once you've saved the file

floral lantern
#

I think it does that automatically (pls someone tell me if it does)

ocean rivet
#
jresolve.exe --output-directory libraries @whatever_you_called_the_file
floral lantern
ocean rivet
#

yep

floral lantern
#

woo

ocean rivet
#

so now you can just keep adding dependencies to that file

hazy oyster
ocean rivet
#

and run that command whenever you need to download new ones

#

just like a (local) pip install

floral lantern
#

man I hate java 😄

ocean rivet
hazy oyster
ocean rivet
#

anywho that is my tool

floral lantern
#

o

#

no

#

fucking

#

way

#

my

#

program

#

works

ocean rivet
#

Thats good

half horizon
#

why not teaching smth like maven/gradle? @ocean rivet

ocean rivet
half horizon
#

its not useful though

ocean rivet
#

what do you mean, he just said "o no fucking way my program works"

#

seems to have been useful

half horizon
#

but not long term

soft cradle
#

(OP wanted to try emcues tool)

#

And OP knows he should learn maven

hazy oyster
soft cradle
half horizon
#

but learning it for dependency management isnt that hard

soft cradle
#

Yeah

ocean rivet
#

@floral lantern As you see, those commands you ran are a very strange level of controversial. Its a wide world out there

soft cradle
#

Neither is gradle, just creating a build.gradle intellij will prompt to import the gradle project and setup gradle for you

hazy oyster
soft cradle
#

Then in the dependency {} just add the c&p line from maven. Done

hazy oyster
#

You should never teach to a newcomer something extremely niche first

#

@floral lantern and now I advise you to learn maven

floral lantern
#

I'm free

ocean rivet
#

or add some files manually

#

but basically there will be a file called pom.xml that looks like this

#
<?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>org.example</groupId>
    <artifactId>untitled94</artifactId>
    <version>1.0-SNAPSHOT</version>

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

</project>
#

and you can add dependencies to this file

#
<?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>org.example</groupId>
    <artifactId>untitled94</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20231013</version>
        </dependency>
    </dependencies>
</project>
#

and intellij will know about them