0

I added the opencsv jar to my classpath classpath

My code is as follows:

import java.io.File;
import java.io.FileReader;
import java.io.FileOutputStream;

import com.opencsv.CSVReader;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.util.Scanner;

public class CreateDocument
{
    public static void main(String[] args)throws Exception
    {
        CSVReader reader = new CSVReader(new FileReader("invoicedetails.csv"));
        String [] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            // nextLine[] is an array of values from the line
            System.out.println(nextLine[0] + nextLine[1] + nextLine[2]);
        }
    }
}

Now the problem occurs when I try to run the jar from command line, I create the jar by clicking build -> build artifacts I get the following message in my commmand line: commandline

For some extra clarity this is how my solution looks like in IntelliJ: intellij solution

Dennis
  • 3,044
  • 2
  • 33
  • 52
  • see this [answer](http://stackoverflow.com/a/5020566/180100) –  Mar 04 '16 at 10:26
  • 1
    You need to set the classpath when starting the application from the commandline. – Thomas Mar 04 '16 at 10:30
  • in my example this would be : java -classpath InvoiceBuilder.jar com.creaw.CreateDocument right? Entering this gives me the same error result – Dennis Mar 04 '16 at 10:30
  • That depends on where the jar is located, i.e. if it isn't in the local directory you need the path as well. And you also need all the other jars (5 it seems from your screenshots). – Thomas Mar 04 '16 at 10:31

2 Answers2

0

The jar file only contains the classes that you code in the IDE project. You need to specify in the java command the classpath with all the dependencies.

java -classpath="PATH_TO_OPENCSV"-jar InvoiceBuilder.jar

Francesc Lordan
  • 519
  • 4
  • 24
  • How do I embed them in the jar file so that I dont have to type that anymore? I just awnt to be able to run the jar with java -jar InvoiceBuilder.jar <> – Dennis Mar 04 '16 at 10:32
  • 1
    I'm not an IntelliJ user, but probably there's some way to create a jar with all the dependencies in it. Check the META-INF/MANIFEST.MF file inside the jar. If there's a Class-Path attribute you need to copy the dependency jars in the same folder structure. (normally inside lib) – Francesc Lordan Mar 04 '16 at 10:37
  • the manifest file only contained the following lines:Manifest-Version: 1.0 Main-Class: com.creaw.CreateDocument So i added the Class-Path: com.opencsv line, the error still remains though – Dennis Mar 04 '16 at 11:38
0

As others pointed you are missing the dependencies in the created jar. You can create a "fat" jar in Idea. See this blog post. The post is old but should apply to new versions also.

gfelisberto
  • 1,655
  • 11
  • 18