Java – Get the name or path of a running JAR file

In Java, we can use the following code snippets to get the path of a running JAR file. // static String jarPath = ClassName.class .getProtectionDomain() .getCodeSource() .getLocation() .toURI() .getPath(); // non-static String jarPath = getClass() .getProtectionDomain() .getCodeSource() .getLocation() .toURI() .getPath(); Sample output. Terminal /home/mkyong/projects/core-java/java-io/target/java-io.jar 1. Get the path of running JAR 1.1 Create an executable …

Read more

Spring Boot non-web application example

In Spring Boot, to create a non-web application, implements CommandLineRunner and override the run method, for example : import org.springframework.boot.CommandLineRunner; @SpringBootApplication public class SpringBootConsoleApplication implements CommandLineRunner { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootConsoleApplication.class, args); } //access command line arguments @Override public void run(String… args) throws Exception { //do something } } 1. …

Read more

Eclipse – How to know this class belongs to which JAR

For example, I want to know this SpringBootApplication class belongs to which JAR or dependency : import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } } Solution In Eclipse IDE, double clicks on the class name, press CTRL + SHIFT + T (win/*nix) or …

Read more

IntelliJ IDEA – How to know this class belongs to which JAR

For example, how to find out this FileUploadBase class belongs to which JAR or dependency? //… import org.apache.tomcat.util.http.fileupload.FileUploadBase; @ExceptionHandler(FileUploadBase.FileSizeLimitExceededException.class) public String handleError3(Exception e, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("message", e.getCause().getMessage()); return "redirect:/uploadStatus"; } Solution In IntelliJ IDEA, double clicks on the class name and press CTRL + n (win/*nix) or CMD + n (mac) to prompt out …

Read more

Spring Boot – Which main class to start

If Spring Boot project contains multiple main classes, Spring Boot will fail to start or packag for deployment. Terminal $ mvn package #or $ mvn spring-boot:run Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run failed: Unable to find a single main class from the following candidates [com.mkyong.Test, com.mkyong.SpringBootWebApplication] -> [Help 1] Maven …

Read more

Gradle – Create a Jar file with dependencies

In this tutorial, we will show you how to use Gradle build tool to create a single Jar file with dependencies. Tools used : Gradle 2.0 JDK 1.7 Logback 1.1.2 1. Project Directory Create following project folder structure : By default, Gradle is using the standard Maven project structure. ${Project}/src/main/java/ ${Project}/src/main/resources/ ${Project}/src/test/java/ 2. Java Files …

Read more

Java – Read a file from resources folder

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream. // the stream holding the file content InputStream is = getClass().getClassLoader().getResourceAsStream("file.txt"); // for static access, uses the class name directly InputStream is = JavaClassName.class.getClassLoader().getResourceAsStream("file.txt"); The …

Read more

How to create a jar file with Maven

In this tutorial, we will show you how to use Maven build tool, to create a single executable Jar, and how to deal with the project’s dependencies. Tools used : Maven 3.1.1 JDK 1.7 log4j 1.2.17 Joda-time 2.5 Eclipse 4.3 1. Create a simple Java project Create a Java project from the Maven quick start …

Read more

How to make a Java exe file or executable JAR file

In this tutorial, we will show you how to create a executable JAR – When you double click on it, it runs the defined main class in manifest file. 1. AWT Example Create a simple AWT Java application, just display label and print out some funny characters ~ AwtExample.java package com.mkyong.awt; import java.awt.Frame; import java.awt.Label; …

Read more

The Java Archive Tool (JAR) Examples

Here’s the project structure. /workspace/test/classes/com/mkyong/awt/AwtExample.class /workspace/test/classes/com/mkyong/awt/AwtExample2.class /workspace/test/classes/com/mkyong/awt/AwtExample3.class /workspace/test/classes/manifest.txt P.S Assume you are in “/workspace/test/classes/“ 1. Create a jar file -c create new archive -v generate verbose output on standard output -f specify archive file name 1.1 Create a Jar file which include AwtExample.class only. jar -cvf test.jar com/mkyong/awt/AwtExample.class 1.2 Create a Jar file which include …

Read more

How to add a manifest into a Jar file

In Java, you can use manifest file to define application’s entry point, adding classpath or package version for a JAR file. In this short tutorial , we will show you how to add a custom manifest file into a Jar file. 1. Project Structure Assume this is your project folder structure /workspace/test/classes/com/mkyong/awt/AwtExample.class /workspace/test/classes/manifest.txt 2. Jar …

Read more