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

时间:2018-10-01 19:27:02

标签: java intellij-idea jar

我读了this帖子,并按照步骤操作,但仍然无法运行它。

我尝试按以下方式运行

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

此后需要做什么?这些依赖是什么?另外,我正在共享pom.xml,其中包含一些项目所需的库。

enter image description here

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

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>

2 个答案:

答案 0 :(得分:0)

我用Java创建了一个Test项目,并构建并执行了jar。

最初,项目的结构如下图所示。 out目录仅包含生产目录。 Initial

现在单击File-> Project Structure-> Artifacts。单击左上角左导航窗格旁边的+符号。

enter image description here

现在单击+符号,然后从具有依赖性的模块中选择Jar->。...将会出现一个对话框,如下所示:

enter image description here

现在选择Main Class,然后选择radio button前面的extract to the target JAR,然后单击OK。您将看到以下enter image description here

选中Include in project build checkbox,然后单击Apply->Ok

enter image description here

单击Build->Build Project。现在您可以看到out directoryartifacts sub-directory,它将有相应的jar

enter image description here

现在intelliJ中的终端上进入存在jar的目录,并执行命令java -jar JAR_NAME.jar,您可以看到主要功能已执行。

答案 1 :(得分:0)

所以我不确定为什么我的IntelliJ生成的JAR无法执行并获取Could not load main class,所以我在命令行上尝试了整个Java项目并生成了对我有用的JAR。

以下是步骤:

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。还要安装以下内容

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

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。编辑代码文件和pom.xml。您可以在IntelliJ中打开项目并进行编辑。

5。 MAVEN BUILD:从Java项目的根目录运行

java-project$mvn package or mvn clean package

6。删除这些在构建后创建的文件

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。运行JAR

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