Reddi
Reddi

Reputation: 743

How to create batch file and execute jar file with arguments

How create a batch file to a jar file that contains a SortFile.java class that is responsible for sorting the content of a file. This class is taking 2 arguments the first one is sort and the second is file name. I would like to create a batch file that will be moved to cmd and after the path to this batch file user can pass those 2 args. The .jar let's say is on C:\dir\SortFile.jar and I would like to execute this batch from any location. Could someone help me, I never wrote a batch files that executes jars. I google it, but all examples I found did not explain it as well, there was no example for that case I need it.

Upvotes: 1

Views: 267

Answers (1)

GhostCat
GhostCat

Reputation: 140457

Let's go step by step:

The .jar let's say is on C:\dir\SortFile.jar

So your batch file probably should contain java -jar C:\dir\SortFile.jar sort fileToSort (assuming of course that you compiled that Java code already, and that you build the JAR file so that it can be called that way)

I would like to execute this batch from any location.

Then that batch file should be located somewhere in your %PATH%.

If you prefer to use the CLASSPATH, see here for guidance.

Upvotes: 1

Related Questions