0

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?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Step
  • 463
  • 1
  • 4
  • 19
  • If you are using any external libraries as dependencies, they need to be included in the classpath as well. Do you have such dependencies – Klaus Apr 11 '20 at 11:43
  • I indeed have an external library that I use for handling JSON objects. – Step Apr 11 '20 at 11:48
  • Try and see javac –classpath – Klaus Apr 11 '20 at 11:51
  • This layout looks like a Maven project. Are you sure you don't have a pom.xml for it? Then your command line is `mvn run`. – Thilo Apr 11 '20 at 11:58
  • This is a Maven project and I can't believe I didn't consider that to be a relevant thing to mention!! My bad. I tried `mvn run` though and it returned an `Unknown lifecycle phase "run".` error. – Step Apr 11 '20 at 12:04
  • Maybe the project does not define how to run it. But you should be able to at least `mvn compile`, and probably also `mvn package` into a jar. Maybe you can run that one, then. – Thilo Apr 11 '20 at 12:14
  • Managed to get it running @Thilo, see my answer. Thanks for the help! – Step Apr 11 '20 at 12:38

3 Answers3

1

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

Step
  • 463
  • 1
  • 4
  • 19
0

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.

0

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
Ritika Goel
  • 285
  • 3
  • 19