简体   繁体   English

从jar文件运行类

[英]Running class from jar file

I added the opencsv jar to my classpath 我将opencsv jar添加到我的类路径中 类路径

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: 现在,当我尝试从命令行运行jar时,出现了问题,我通过单击build-> buildartifacts创建了jar,我在命令行中得到了以下消息: 命令行

For some extra clarity this is how my solution looks like in IntelliJ: 为了更加清楚,这是我的解决方案在IntelliJ中的外观: IntelliJ解决方案

The jar file only contains the classes that you code in the IDE project. jar文件仅包含您在IDE项目中编码的类。 You need to specify in the java command the classpath with all the dependencies. 您需要在java命令中指定具有所有依赖项的类路径。

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

As others pointed you are missing the dependencies in the created jar. 正如其他人指出的那样,您缺少所创建jar中的依赖项。 You can create a "fat" jar in Idea. 您可以在Idea中创建一个“胖” jar。 See this blog post . 请参阅此博客文章 The post is old but should apply to new versions also. 该帖子很旧,但也应适用于新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM