在没有ecplise的情况下执行java / maven项目

时间:2018-05-30 01:33:58

标签: java maven command-line

我创建了一个Maven项目。有没有办法在不使用eclipse的情况下执行程序?我尝试运行javac main.java但我收到此类错误package org.json does not exist

我错过了什么?

项目的结构是这样的:

program
   src
     com
        main.java
        otherClass.java
   pom.xml
   .classpath
   .project

2 个答案:

答案 0 :(得分:2)

您可以使用Apache Maven Jar Plugin。这是为了创建项目的JAR并使其更容易从命令行运行

指定主类:

    Public Function GetUser(Optional whatpart = "username")
        Dim returnthis As String
        If whatpart = "username" Then GetUser = Environ("USERNAME"): Exit Function
        Set objSysInfo = CreateObject("ADSystemInfo")
        Set objUser = GetObject("LDAP://" & objSysInfo.USERNAME)
        Select Case whatpart
            Case "fullname": returnthis = objUser.FullName
            Case "firstname", "givenname": returnthis = objUser.givenName
            Case "lastname": returnthis = objUser.LastName
            Case Else: returnthis = Environ("USERNAME")
        End Select
        GetUser = returnthis
    End Function

当您运行<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> 时,它会创建一个JAR或您的项目,并使用mvn clean install

运行它

查看此link

希望它有所帮助!

答案 1 :(得分:0)

在我的建议中:

#1创建maven项目

#2使用程序集插件创建FAT jar(参见下面的pom)

#3添加exec插件(参见下面的pom)

#4创建Main类(请参阅下面的MainApp.java代码)

#5转到项目目录并运行maven命令

mvn clean install -DskipTests

这将生成胖jar名称sample-jar-with-dependencies.jar

#6运行以下maven命令以运行MainApp类

mvn exec:exec

#7完成!

希望这会有用。

<强>的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>java-sample-project</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <jdk.version>1.8</jdk.version>
    <junit.version>4.11</junit.version>
    <fat-jar-name>target/sample-jar-with-dependencies.jar</fat-jar-name>
    <main-class-name>com.java.example.MainApp</main-class-name>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <finalName>sample</finalName>
    <plugins>
        <!-- Set a compiler level -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>

        <!-- Added JUnit support -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
        </plugin>

        <!-- Maven Exec Plugin to run main class -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>sample-id-1</id>
                    <!-- <phase>prepare-package</phase> -->
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
                <mainClass>com.java.example.MainApp</mainClass>
                <arguments>
                    <argument>-classpath</argument>
                    <argument>${fat-jar-name}</argument>
                    <argument>${main-class-name}</argument>
                </arguments>
            </configuration>
        </plugin>

        <!-- Maven Assembly Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- MainClass in mainfest make a executable jar -->
                <archive>
                    <manifest>
                        <mainClass>com.java.example.MainApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <!-- bind to the packaging phase -->
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<强> MainApp.java

 package com.java.example;

 public class MainApp {
 public static void main(String[] args) {
    System.out.println("Hello World...");
}
}

输出mvn exec:exec命令

    $ mvn exec:exec
    Picked up _JAVA_OPTIONS: -Xmx512M
    [INFO] Scanning for projects...
    [INFO]
    [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building java-sample-project 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- exec-maven-plugin:1.2.1:exec (default-cli) @ java-sample-project ---

    Hello World...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.841 s
    [INFO] Finished at: 2018-06-06T21:29:32+05:30
    [INFO] Final Memory: 6M/27M
    [INFO] ------------------------------------------------------------------------