How to generate jar file from spring boot application using gradle? - java

I am creating a spring boot application using gradle. I need to create
a jar which include all needed libraries for deploying on (aws) ec2
instance.
I am trying to create a jar by...
1)click on project structure
2)then click on Artifact
3)click on + -> jar -> from modules with dependencies -> select project -> select main class
4)click on ok with default settings
5)click on build tab
6)I got a jar file but it doesn't contain classes.

Use Terminal/Command Prompt to generate jar using Gradle
To generate jar without running test cases
./gradlew build -x test
If you want to run the test cases before creating jar, then
./gradlew build

First make sure you have downloaded gradle and set up in your system variable.
to check this simply run gradle -v in your cmd.
Under System Variables select Path, then click Edit. Add an entry for
C:\Gradle\gradle-6.5.1\bin. Click OK to save.
Now you simply goes to project directory and run
./gradlew build -x test
Now you get your executable jar file

Related

how to run/execute a java code(maven project) outside the IDE

I am new to programming, there is a maven project with java code that does certain testing and it runs regularly through Jenkins pipeline, it runs the tests on an environment (linux machine).
Now what I am struggling and not knowing where to start is how can I test this code manually knowing that it has different Mains (i.e Main1 , Main2 , Main3) and I want to run a certain Main not all, for example only Main1 which tests specific thing.
ps: I have the code in Intellij, but I want to run it outside intellij, I want to run it on the environment I'm testing (linux maachine).
first you have to build the maven project using this command in your project directory:
mvn package
After a successful build, you will see a .jar file has created in target folder same as your package name and version.
finally you have to RUN the project.
To run the project use this command:
java -cp target/jarfileName.jar path_of_the_project_startup
Done.
First, you need to build your maven project. Navigate to the project folder (you must have the project root pom.xml there) open a terminal and to build it type:
mvn clean package
Depending on your project structure, a successful run of this command will result in several target folders at different levels for your modules and possibly .jar files inside them. Navigate into the target folder of the module in which your entry point (main) is and check jar file name. Let's call it jarname.jar for simplicity.
In case you have several entry points in the same jarname.jar, you can run them like this:
java -cp jarname.jar Main1
java -cp jarname.jar Main2
java -cp jarname.jar Main3
If you want to run from terminal and still debug from IntelliJ, run app with:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
and then create a 'Remote JVM Debug' config in IntelliJ on same port (5005), adding some break points and 'Debug'.
As an alternative, you can create Run Config 'Java Scratch' from IntelliJ to run/debug your app.

How to create an artifact jar using the command line only?

I have a java project and using IntelliJ in project settings I've created an Artifact JAR. My project is of the form:
lib/MyInterface.jar
src/com/test/MyTest.java (does not contain a main method)
Where MyTest.java references MyInterface.jar.
When I run [Build -> Build Artifacts -> MyTest.jar -> Build] IntelliJ creates a jar that simply contains MyInterface.class and MyTest.class. I'm using this JAR as a library in another project.
What I need is to be able to create this JAR without having to use IntelliJ or Maven or anything other than javac and jar (due to constraints on the machine this code will be deployed to). Is this possible and if so how can I go about doing this using the command line only?
jar cf ${name for the created jar} input-file(s)
or cd into your package directory then use:
jar cf ${name for the created jar} *

Spring Boot Run Jar file in linux

I have developed a Spring Boot Application using Restfull API. The application working perfectly in STS. But now i am trying to run a jar file in linux, any idea.
You can generate runnable jar file using 2 ways.
a. Exporting as JAR file from STS.
Right click on project in STS --> click on Export --> search for jar
--> click on JAR file --> select project --> select resource to export --> Select checkbox : Export Java source files and resources --> select the export destination --> give a name for JAR file (for
example abcd.jar) click finish to generate JAR file.
b. Using Maven.
First, make sure that you have the repackage goal included in your
build setup, then use the Maven package target. The simplest way
to do this is to run mvn package from the command line (you may need
to install the Maven CLI package for your OS); you can also
right-click the POM in Eclipse and "Run As" to execute specific Maven
operations from within Eclipse.
To run JAR file open command/terminal prompt and reach to destination where JAR file was created. then execute command java -jar abcd.jar
Can you follow the steps
check the version which you have installed in Linux
java -version
Install the JDK corresponding to your application version which you have created application
sudo apt-get install openjdk-version-what you want JDK verson -jre
// example - sudo apt-get install openjdk-7-jre
Run the application simply typing
java -jar Minecraft.jar

Exporting Spring Boot application as JAR file in eclipse

I'm using the Spring STS in Eclipse to create a simple web-based spring boot project. I can run it fine in Eclipse, but when I try to export it as a JAR file I get:
rg.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
My public static void mainis located in Application.java, with the #SpringBootApplication annotation.
I've double checked all the Maven dependencies a hundred times.
What am I doing wrong?
Most likely, you're using the built-in Eclipse exporter to generate your jar, which only includes the target files actually produced in that project. In order to have a "fat" (standalone executable) jar, you need to use the Spring Boot Maven or Gradle plugin to "repackage" the jar.
First, make sure that you have the repackage goal included in your build setup, then use the Maven package target. The simplest way to do this is to run mvn package from the command line (you may need to install the Maven CLI package for your OS); you can also right-click the POM in Eclipse and "Run As" to execute specific Maven operations from within Eclipse.
It is a single line command, on window 7/10 machine, with command prompt to your project folder (Inside your project workspace). I do not do with Eclipse IDE POM maven goals, but you can do with maven goal there also. ON window machine I prefer cmd.exe for exporting and running.
mvnw clean package
on unix kernel based
./mvnw clean package
You have to go inside workspace and than to the project root folder. You will see a maven wrapper mvnw, with that you don't need to have maven installed and .mvn folder at the same level provides necessary jar for that.
For a project
D:\workspace\Zuteller Workspace\zusteller>mvnw clean package
it will create zusteller-0.0.1-SNAPSHOT.jar in the target folder at the same level.
D:\workspace\Zuteller Workspace\zusteller>java -jar target\zusteller-0.0.1-SNAPSHOT.jar
You can run self-contained application(embedded Tomcat) and access at localhost:8080/your project

How to create a jar with external libraries included in Eclipse?

I am done with the project which connects to database (MySQL). Now I want to export the project as jar. But I don't know how to include its external dependencies? Is there any way of doing it in Eclipse or should I use any scripts for that?.
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. The selected class is started when you 'run' the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included (for maven projects, search here).
You could use the Export->Java->Runnable Jar to create a jar that includes its dependencies
Alternatively, you could use the fatjar eclipse plugin as well to bundle jars together
You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.
Personally,
None of the answers above worked for me, I still kept getting NoClassDefFound errors (I am using Maven for dependencies). My solution was to build using "mvn clean install" and use the "[project]-jar-with-dependencies.jar" that that command creates. Similarly in Eclipse you can right click the project -> Run As -> Maven Install and it will place the jars in the target folder.
If you want to export all JAR-files of a Java web-project, open the latest generated WAR-file with a ZIP-tool (e.g. 7-Zip), navigate to the /WEB-INF/lib/ folder. Here you will find all JAR-files you need for this project (as listed in "Referenced Libraries").
While exporting your source into a jar, make sure you select runnable jar option from the options. Then select if you want to package all the dependency jars or just include them directly in the jar file. It depends on the project that you are working on.
You then run the jar directly by java -jar example.jar.
To generate jar file in eclipse right click on the project for which you want to generate, Select Export>Java>Runnable Jar File,
Its create jar which includes all the dependencies from Pom.xml, But please make sure license issue if you are using third-party dependency for your application.
If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into generated jar" -- > Finish.
Or
If you have a web project then put all the jars in web-inf/lib folder and do the same step.
Before exporting, click on Buildpath and configure java buildpath and add external jars inside the library. Then try to export as a runnable jar.

Categories

Resources