Dojo_user
Dojo_user

Reputation: 281

Creation of a .bat file of a java application

I am a bit confused about the process to create a .bat file of a java application. I have exported the executable jar using IDE say Application.jar in C: directory. Then I have written two lines in a .txt file as stated below and saved it as .bat file in the same directory where i have my application.jar. But on double click of the .bat file, the application is not getting executed.

.BAT file code

javac Application.java
java -cp . Application

Note: I have also set the JRE and JDK path in my environment variables till bin path in My Computer properties. But it is not working. Can someone suggest me how can I fix this, because I want to execute my code by doubleclickng on a .bat file. It will be nice if someone can provide me every step I need to follow to accomplish this as I havent ever done this before.

Thanks ,

Upvotes: 2

Views: 1220

Answers (2)

Syam S
Syam S

Reputation: 8499

Your .bat is just fine. When you double click it might be executing and then closes. This is because your program might not have any UI and it isnt waiting for any input. To verify this take a command prompt and then execute your bat file via that.

In other case I assume that you have a java class called Application and you need to run this via a batch file. In that case if the class have a main method then you just need one line in .bat file

java -cp <the path to class file> Application

So you might be using a javac just to take advantage of class path as current directory. So when you say

javac Application.java
java -cp . Application

It compiles the class to current folder and set that as class path and then execute. This is absolutely file as long as the Application.java doesnt have any third party dependency. But in this case again you need not set -cp to . (current directory will be taken as classpath automatically unless otherwise specificed). So below will also work fine.

javac Application.java
java Application

I support Jurgen reply. If you have an executable jar file and a jre in path then double clicking it will run the application. The META-INF folder inside the jar will have a MANIFEST.MF file which uses a property called Main-Class: to specify the main executing class. And on double clicking this class gets executed. However its only useful if you have a UI. Else it'll also have no effect.

In all these context the Application.jar you mentioned is irrelevant. If that is a third party jar that you need to run the you should include that in -cp argument.

Upvotes: 2

Jurgen
Jurgen

Reputation: 2154

The first line in your batch file is attempting to compile your program !? The second line is attempting to run the Application.class file.

What you want if you have produced an executable jar file is:

java -jar Application.jar

But you don't really need the batch file at all. If you double click on the jar file and it runs your program then you can just create a shortcut to it.

Upvotes: 3

Related Questions