0

I have a simple class with a main method. This is not my main class in the application but a tool. I want to run it like

java -cp mySpringBoot.jar com.pkg1.pkg2.pkg3.mytool

I can see that this class is in there in the same package but under BOOT-INF/classes/com/pkg1/pkg2/pkg3/mytool.class

I tried using the BOOT-INF/classes folder as well but it keeps saying

Error: Could not find or load main class ... com.pkg1.pkg2.pkg3.mytool Edit: My tool class that is inside the spring boot jar is very simple, it is not in the same package as the root where my SpringBootApplication class resides.

package com.comp.mypackage.tools;

//imports
public class MyTool{

public static void main (String args[]) {
......
}

}
sarmahdi
  • 1,098
  • 4
  • 21
  • 61

2 Answers2

1

Take a look at this stackoverflow post How to run a JAR file

It will show you how to use a manifest file (where you define your main method), how to package (create) the jar, and run the jar.

chocksaway
  • 870
  • 1
  • 10
  • 21
  • I know how to do that in a normal Jar. thank for the link.; But i want to do it in a Springboot jar which already has a main class and spring boot writes to the manifest file. Even in a normal jar you can do java -cp myjar.jar com.mypackage.myclasswithmain and it runs. but in spring boot it does not. If you do java -jar myjar.jar to a spring boot it runs the class in the manifest. – sarmahdi May 16 '18 at 21:07
  • The only way I know of running a "main method", in a Spring Boot JAR, is to use a manifest file. I've added another answer to illustrate this. – chocksaway May 17 '18 at 11:23
0

Spring boot creates a "fat JAR" with maven (with all depenedencies). For an example, if you go to the Spring Boot guide https://spring.io/guides/gs/maven/

Clone the git repo. mvn package (which creates the JAR file). Look in the JAR file - inside "META-INF/MANIFEST.MF". There is a "Main-Class: hello.HelloWorld", which defines the package, class to run:

git clone https://github.com/spring-guides/gs-maven.git
cd gs-maven/complete

mvn package
cd target/
jar xvf gs-maven-0.1.0.jar
less META-INF/MANIFEST.MF

-----
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: ********
Created-By: Apache Maven 3.5.2
Build-Jdk: 1.8.0_121
Main-Class: hello.HelloWorld
chocksaway
  • 870
  • 1
  • 10
  • 21