简体   繁体   English

如何从命令行运行从Intellij创建的Java JAR

[英]How can I run Java JAR created from Intellij from command line

I read this SO post and followed the steps but still I am unable to run it. 我阅读了这篇 SO帖子并按照步骤进行操作,但仍然无法运行它。

I tried running it as follows 我尝试如下运行

Directory_where_jar_is_located $ java -jar Main.java
Error: Unable to access jarfile IcerbergRun
Directory_where_jar_is_located$ java -jar ProjectName.jar 
Error: Could not find or load main class IcebergRun

What needs to be done after this? 此后需要做什么? What are those dependencies? 这些依赖是什么? Also, I am sharing my pom.xml that has some bunch of libraries that are required for my project. 另外,我共享我的pom.xml,其中包含一些项目所需的库。

在此处输入图片说明

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0

<groupId>SparkIceberg</groupId>
<artifactId>SparkTut</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <!--<logger name="org.apache.catalina.core" level="OFF" />-->
            </configuration>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>


<dependencies>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.417</version>
    </dependency>
    <dependency>
        <groupId>com.github.Netflix.iceberg</groupId>
        <artifactId>iceberg-spark</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.Netflix.iceberg</groupId>
        <artifactId>iceberg-data</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.Netflix.iceberg</groupId>
        <artifactId>iceberg-common</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.Netflix.iceberg</groupId>
        <artifactId>iceberg-api</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.Netflix.iceberg</groupId>
        <artifactId>iceberg-core</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.Netflix.iceberg</groupId>
        <artifactId>iceberg-parquet</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.Netflix.iceberg</groupId>
        <artifactId>iceberg-orc</artifactId>
        <version>0.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-aws</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.parquet</groupId>
        <artifactId>parquet-common</artifactId>
        <version>1.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.parquet</groupId>
        <artifactId>parquet-column</artifactId>
        <version>1.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.parquet</groupId>
        <artifactId>parquet-hadoop</artifactId>
        <version>1.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro</artifactId>
        <version>1.8.2</version>
    </dependency>
</dependencies>

I created a Test project in java and built and executed the jar. 我用Java创建了一个Test项目,并构建并执行了jar。

Initially the structure of project looked as shown below. 最初,项目的结构如下图所示。 The out directory contains only production directory. out目录仅包含生产目录。 初始

Now click on File-> Project Structure->Artifacts. 现在单击文件->项目结构->工件。 Click on + sign next to left navigation pane in the upper left corner. 单击左上角左导航窗格旁边的+号。

在此处输入图片说明

Now click on + sign and select Jar->from module with dependencies.... A dialog box will appear as shown below: 现在单击+号,然后从具有依赖项的模块中选择Jar-> from....。将出现一个对话框,如下所示:

在此处输入图片说明

Now select the Main Class and select the radio button in front of extract to the target JAR and click OK . 现在选择Main Class然后选择要解extract to the target JAR前面的radio button ,然后单击OK You will see the following 您将看到以下内容 在此处输入图片说明 :

Check the Include in project build checkbox and click Apply->Ok . 选中“ Include in project build checkbox ,然后单击“应用”- Apply->Ok

在此处输入图片说明

Click on Build->Build Project . 点击Build->Build Project Now you can see that out directory has artifacts sub-directory which will have corresponding jar 现在您可以看到out directory具有artifacts sub-directory将具有相应的jar

在此处输入图片说明

Now on the terminal in intelliJ go inside directory where jar is present and execute the command java -jar JAR_NAME.jar and you can see that main function is executed. 现在,在intelliJ的终端上,进入存在jar的目录,并执行命令java -jar JAR_NAME.jar ,您将看到main函数已执行。

So I am not sure why my IntelliJ produced JAR was unable to execute and getting Could not load main class so I tried my entire java project on command line and generated the JAR that worked for me. 所以我不确定为什么我的IntelliJ生产的JAR无法执行并且Could not load main class所以我在命令行上尝试了整个Java项目并生成了对我有用的JAR。

Here are the steps: 步骤如下:

1. Install maven on ec2 https://gist.github.com/sebsto/19b99f1fa1f32cae5d00 1.在ec2上安装maven https://gist.github.com/sebsto/19b99f1fa1f32cae5d00

$sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
$sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo
$sudo yum install -y apache-maven
$mvn —version

2. Install the following as well 2.还要安装以下内容

$sudo yum install java-1.8.0-openjdk-devel
$yum install zip

3. Do this to create maven project http://www.mkyong.com/maven/how-to-create-a-java-project-with-maven/ 3.执行此操作以创建Maven项目 http://www.mkyong.com/maven/how-to-create-a-java-project-with-maven/

$mvn archetype:generate -DgroupId=com.package.name -DartifactId=java-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

4. Edit code files and pom.xml. 4.编辑代码文件和pom.xml。 You can open the project in IntelliJ and edit. 您可以在IntelliJ中打开项目并进行编辑。

5. MAVEN BUILD: Run from root dir of your java project 5. MAVEN BUILD:从Java项目的根目录运行

java-project$mvn package or mvn clean package

6. Remove these files created after build 6.删除构建后创建的这些文件

https://gist.github.com/hkhamm/88923412992d284580ea https://gist.github.com/hkhamm/88923412992d284580ea

java-project$zip -d target/iceberg-project-1.0-SNAPSHOT.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF

7. Run JAR 7.运行JAR

java-project$java -cp target/java-project-1.0-SNAPSHOT.jar com.project.name.App

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

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