0

My work is currently having me take an intro to Java course. While I'm familiar with Python and python packages and virtual environments, I'm having trouble translating some of this to Java.

The course comes with its own IDE, which circumvents some of these issues, but the IDE won't launch and I'd rather understand packages from the start anyways. Once I get past this bump I can continue the tutorial.

My current directory structure looks like this:

project_dir  
├── edu  
│   └── edu_package  
│      ├── all_files_in_edu_package.java  
├── unicode.txt  
├── Main.class  
├── Main.java  

The Main class relies on classes contained in the edu_package, and they seem to import well enough to throw an exception.

Here's the exception:

error: package org.apache.commons.csv does not exist
import org.apache.commons.csv.CSVFormat;

It seems i need a JAR file, but I cannot figure out if there's a pip install org or pypi equivalent in Java.

Edit: The exception isn't throw in Main, it's thrown in the edu_package which is imported in Main.

Osuynonma
  • 489
  • 1
  • 7
  • 13
  • 2
    You may use `Maven` or `Gradle` as the dependency management tool. At least. you could download the JAR manually from https://mvnrepository.com/artifact/org.apache.commons/commons-csv – chenzhongpu Apr 28 '22 at 02:02

1 Answers1

2

Follow this link of SO for understanding how to use jar files.

You need to specify the downloaded jar files path while compiling or running you code.

Please download Apache Commons CSV from here after download extract the jar file commons-csv-1.9.0.jar from the archive to a directory.

import org.apache.commons.csv.CSVFormat;

class MainClass{
    public static void main(String ... $){
        final var out = System.out;
        out.println("Hell o world");
    }
}

Compiling :

javac -cp '.:/home/devp/Documents/commons-csv-1.9.0.jar' MainClass.java 

Running :

java -cp '.:/home/devp/Documents/commons-csv-1.9.0.jar' MainClass
Udesh
  • 2,415
  • 2
  • 22
  • 32
  • 1
    +1: Good response. I was going to reply, too. But you beat me to it. IMPORTANT POINT (for the OP's benefit): In Java, you don't "install" a package. Instead, you can create your own classes (which often belong to a "package", as reflected in their relative subdirectory). Or you add a .jar to the app's CLASSPATH (as you've illustrated above). "Packages" are implicit in how class files are organized in the .jar. Here's a good tutorial: [Java Packages](https://www.w3schools.com/java/java_packages.asp) – paulsm4 Apr 28 '22 at 02:06