简体   繁体   中英

How to compile and execute a Java project from the command line?

I'm trying to compile and run a java project done in IntelliJ from the command line and I'm a bit perplexed on how to do it. This is my project structure:

├───src
│   ├───main
│   │   ├───java
│   │   │   │   Main.java // Main method's in here.
│   │   │   │
│   │   │   ├───objectclasses
│   │   │   │       Application.java
│   │   │   │
│   │   │   └───utils
│   │   │           Defs.java
│   │   │           Api.java
│   │   │           FileIO.java
│   │   │
│   │   └───resources
│   │           applicationIDs.tmp
│   │           applications.csv
│   │
│   └───test
│       └───java
└───target
    // .class files compiled by IntelliJ

Going into the command line, navigating to the java directory, and simply typing:

javac Main.java

doesn't work and returns errors saying that all the packages and symbols referred to in the code can't be found. I've also tried navigating to the root directory and running:

javac -d target -sourcepath src src/main/java/Main.java

which returns similar errors. Finally, I tried the following, which outright said "no source files":

$ javac -d target -sourcepath src -cp . 

The error that's being returned:

src\main\java\Main.java:1: error: package objectclasses does not exist
import objectclasses.Application;
                    ^
src\main\java\Main.java:2: error: package utils does not exist
import utils.Defs;
            ^
src\main\java\Main.java:3: error: package utils does not exist
import utils.Api;
            ^
src\main\java\Main.java:4: error: package utils does not exist
import utils.FileIO;
            ^
src\main\java\Main.java:58: error: cannot find symbol
    private static void updateApplicationsFile(List<Application> applications)
                                                    ^
  symbol:   class Application
  location: class Main
src\main\java\Main.java:17: error: cannot find symbol
        Api api= new Api(
        ^
  symbol:   class Api
  location: class Main
src\main\java\Main.java:17: error: cannot find symbol
        Api api = new Api(
        ^
  symbol:   class Api
  location: class Main
src\main\java\Main.java:18: error: cannot find symbol
                Defs.API_JSONRPC,
                ^
  symbol:   variable Defs
  location: class Main
src\main\java\Main.java:19: error: cannot find symbol
                Defs.API_ID,
                ^
  symbol:   variable Defs
  location: class Main
src\main\java\Main.java:20: error: cannot find symbol
                Defs.API_KEY,
                ^
  symbol:   variable Defs
  location: class Main
src\main\java\Main.java:21: error: cannot find symbol
                Defs.API_SESSION_ID,
                ^
  symbol:   variable Defs
  location: class Main
src\main\java\Main.java:22: error: cannot find symbol
                Defs.API_DOMAIN);
                ^
  symbol:   variable Defs
  location: class Main
src\main\java\Main.java:26: error: cannot find symbol
        List<Application> applications = api.getApplicationsFromIDList(applicationIDs);
             ^
  symbol:   class Application
  location: class Main
src\main\java\Main.java:40: error: cannot find symbol
            FileIO.writeIDsToFile(applicationIDs);
            ^
  symbol:   variable FileIO
  location: class Main
src\main\java\Main.java:63: error: cannot find symbol
            FileIO.writeApplicationsToFile(applications);
            ^
  symbol:   variable FileIO
  location: class Main
15 errors

I've never done this sort of thing before so I'm probably missing something obvious. Any help?

So I managed to get the file to run from the command line thanks to Thilo. It was a Maven project, which means that it should be compiled and run via Maven commands. These were the steps I took:

Step 1: Make sure Maven is installed .

Step 2: Open the pom.xml in the root folder of my project.

Step 3: Add the following plugin to the <build><plugins></plugins></build> tags:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Step 4: Navigate to the root folder of the project in the command line.

Step 5: Run mvn compile and mvn package .

Step 6: Take note of the location of the JAR file in the directory specified in the returned logs. In my case, it was:

target\App-1.0-SNAPSHOT-jar-with-dependencies.jar

Step 7: Run the JAR file with java -jar target\App-1.0-SNAPSHOT-jar-with-dependencies.jar

For that, you have to specify the path where you have saved your.java file. if it is in documents(folder) simply write on commande line:

1) cd documents if you have created a folder inside documents then write:

2) cd foldername and then to compile write:

3) javac (space) your filename along with.java extension. (javac stand for java compile) if your project includes a compile-time error it will prompt at this stage. After compilation you have to write:

4) java (space) your filename without extension . because this time we have compile our project. and boom

I used to run my java project this way... but sorry to say I was using notepad not IntelliJ. Hope it helps... According to your question title the answer is right.

Please try below steps: - Export a runnable jar for your project from intellij. Please refer https://stackoverflow.com/q/1082580/12070280 on how to do so. - Make sure your system Path variable is set to java bin. - Navigate to the path where you exported your jar file, and run

java myjarfile.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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