0

I am new to java and facing same old issue while executing the Class/Jar file, i.e. getting error which says "Could not find or load main class com.finance.LoanProcessor", where com.finance.LoanProcessor is my fully qualified class name. I have created many other sample projects, and I can execute both class/jar file in them. Only this project is causing the issue.

Here is what I understand from Java tutorials and StackOverflow solutions for this issue:

  1. While executing the class file directly using 'Java' command, I need to use this syntax: java com.finance.LoanProcessor

Note: I should not use '.class' suffix. In command prompt, I should be in the parent folder which has the 'com' folder, so that the java command can navigate inside the com/finance directory and execute the LoanProcessor class.

  1. While executing the jar file directly using the 'Java' command, I need to use this syntax: java -jar LoanOnboardingSystem-1.0-SNAPSHOT.jar

Note: I should be in the same directory where this jar file is located. Also, the jar should have the META-INF/MANIFEST.MF file, which should have the Main-Class: com.finance.LoanProcessor attribute defined.

I have kept all above notes in mind and tried executing my main class and jar both, but both are failing. While other sample projects are getting executed in same fashion on my same laptop, only this project is giving error. I have asked a few java experts in my office, but they are not able to figure out issue too.

Hence, I have uploaded the whole project on following git repository now: LoanOnboardingProject

Could the community kindly help to check the project and suggest what I am missing?

Few other things which I have done:

  1. I am using IntelliJ IDE. I have built jar with IDE's out of box options, and it gives same error. [This jar is under the 'out' folder in above repository.]

  2. I have created above project as Maven project. I used 'mvn package' command to build the jar, but it gives the same error. [This jar is under the 'target' folder in above repository.]

  3. When I run the program within IntelliJ itself, it works just fine. No issues with that.

Thanks a lot in advance. Any help is much appreciated.

Vaibhav Shah
  • 93
  • 1
  • 10
  • Let maven do the packaging and adminsiter you dependencies. You need to learn maven as well as Java – Stimpson Cat Aug 04 '17 at 09:35
  • @StimpsonCat: thanks for the response. I have used maven quickstart template to create the project, and I have built the jar file using 'mvn package' command too (as mentioned in post as well). Despite that, the same jar file is also giving the same error. – Vaibhav Shah Aug 04 '17 at 09:43
  • Also, the error about dependency would come afterwards, once the execution can at least find the main class and execute the same. I have noticed that in other projects. However, in my case, the 'java' exe is not able to even find the main class com.finance.LoanProcessor. – Vaibhav Shah Aug 04 '17 at 09:45
  • Dont use anything from the target folder. If you have depenendcies on other projects, use the section within the pom.xml or install this dependency in your local maven repository by running mvn install – Stimpson Cat Aug 04 '17 at 09:45
  • @StimpsonCat: Alright, I tried the 'mvn install' just now, and it created the jar file 'LoanOnboardingSystem-1.0-SNAPSHOT.jar' under the folder 'C:\Users\xxx\.m2\repository\com\finance\LoanOnboardingSystem\1.0-SNAPSHOT'. I tried executing the new jar file from this folder, but same error is appearing. [My original project resides on the folder structure 'C:\Users\xxx\IdeaProjects\LoanOnboardingSystem', where the IntelliJ was creating Jar in 'out' folder and maven was creating jar in 'target' folder. Both these files' execution failed too.] – Vaibhav Shah Aug 04 '17 at 09:53
  • Please try using the shade plugin like Mr.AJ proposed. I am using this as well in all kind of projects – Stimpson Cat Aug 04 '17 at 09:56

3 Answers3

2

The problem is, that you main class LoanProcessor implements CommandLineRunner interface. This class is not included in the resulting jar (when built by maven). Therefore it is not on the classpath. And that is the reason, why java can't load the main class (because it references another class that it can't find).

To run the app, you need to add all required classes to the classpath (using -cp parameter of java command). This would be quite complicated in your case, because you would need to add all of your dependencies specified in your pom.xml file. And all jars that these dependencies require. That would probably be a lot of jar files.

You could you maven-shade-plugin to build a jar with all dependencies included.

Here is an example config (add to your pom to the section):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <manifestEntries>
              <Main-Class>com.finance.LoanProcessor</Main-Class>
            </manifestEntries>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
        </transformers>
      </configuration>
    </execution>
  </executions>
</plugin>

Then you will be able to execute the class (when standing in the directory containing the pom.xml):

java -cp target/LoanOnboardingSystem-1.0-SNAPSHOT.jar com.finance.LoanProcessor

or by:

java -jar target/LoanOnboardingSystem-1.0-SNAPSHOT.jar 
Jan
  • 71
  • 3
  • 1
    Voted up for detailed explanation – P.An Aug 04 '17 at 10:01
  • Thanks for the great explanation. This worked for me, and I was able to get the jar with all its dependencies. Marking this as answer. – Vaibhav Shah Aug 07 '17 at 05:55
  • However, one more improvement in the same: when I was running the shaded jar, I got a sprint-boot framework error mentioned in this link: https://stackoverflow.com/questions/38792031/springboot-making-jar-files-no-auto-configuration-classes-found-in-meta-inf. Hence, I followed one of the answers mentioned in the same to use the 'spring-boot-maven-plugin', and it worked like charm too. Even when I don't use the shade plugin and just use 'spring-boot-maven-plugin', now it creates the jar just fine, and I can execute the same from command prompt. – Vaibhav Shah Aug 07 '17 at 05:57
  • I have never worked with Spring Boot framework. According to the spring-boot-maven-plugin documentation, it should do very similar thing to the standard maven's shade plugin. And obviously it adds some Spring specific configuration to the resulting jar file. Therefore it should be better for your project. – Jan Aug 07 '17 at 11:27
1
In you pom.xml replace build with given below. Using maven-shade-plugins.

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
                            <promoteTransitiveDependencies>false</promoteTransitiveDependencies>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <minimizeJar>false</minimizeJar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



To Execute:

java -cp LoanOnboardingSystem-1.0-SNAPSHOT.jar com.finance.LoanProcessor
Anuj jain
  • 493
  • 1
  • 8
  • 26
  • Hello Anuj, thanks for the response. I tried this, but the jar created by it throws error as main class is not found. To resolve the same, I need to add the XML node as suggested by Jan in his answer above. However, then both your and Jan's answers are similar, and both behave in same fashion. – Vaibhav Shah Aug 07 '17 at 06:20
  • How You executing the code. I was able to execute the jar using the command i provided : – Anuj jain Aug 07 '17 at 09:29
  • Aah.. My bad.. I was using the command 'java -jar LoanOnboardingSystem-1.0-SNAPSHOT.jar' instead of the one you had provided. When I use command you have given, the class gets executed (with spring-boot error mentioned in the answer I posted in this post). Sorry for the confusion, and thanks for the answer. I am not able to mark two posts as 'Answer' right now, hence I shall upvote your answer once I have sufficient reputation points. :) – Vaibhav Shah Aug 07 '17 at 09:51
0

The answer provided by Jan above is definitely going to solve many people's problems, and it solved my problem too. However, since I was using the spring boot, I got another error while running the shaded jar: "No auto configuration classes found in META-INF/spring.factories".

Hence, I came across this link on StackOverflow itself, which solved above problem.

The interesting thing is that now when I use the 'spring-boot-maven-plugin' only in the pom.xml, and not the 'maven-shade-plugin', then also the project jar gets executed just fine. Spring-boot-maven-plugin also includes all the required dependencies in jar file and creates a smaller jar than the one created by 'maven-shade-plugin.'

Just in case that link goes down some day, I am posting the same answer here too:

   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.2.RELEASE</version>
      <executions>
       <execution>
        <goals>
         <goal>repackage</goal>
        </goals>
       </execution>
     </executions>
   </plugin>
Vaibhav Shah
  • 93
  • 1
  • 10