Yatin Kanyal
Yatin Kanyal

Reputation: 205

How can I execute .jar file from the terminal without specifying its name

How can I execute .jar file in folder from the Windows cmd without specifying its name.

I have tried below command (as there is only 1 jar in that folder I used *) but its not working(Error: Unable to access jarfile *.jar ).

java -jar *.jar

Upvotes: 2

Views: 1474

Answers (2)

Stephen C
Stephen C

Reputation: 718826

So what you appear to be asking is how to run the command

 % java -jar somelongname.jar

as

 % java -jar *.jar 

to avoid some typing. That's not possible, because neither the Windows CMD shell or the java command is going to expand the *.jar wildcard pattern.

(On Linux / Unix / MacOS, the shell does wildcard expansion before passing arguments to a command. On Windows, it is the responsibility of the command to do this. In practice, it appears that the java command only expands wildcards in the arguments that are going to be passed to your application; see Stop expanding wildcard symbols in command line arguments to Java)

So if you want to avoid typing those pesky characters on Windows, you will need to do something like:

  • write a simple BAT file to run "java -jar somelongname.jar", or
  • write a clever BAT file to identify and run a JAR file that matches "*.jar", or
  • use Powershell.

For what it is worth, I think what you are trying to do is rather dangerous. This is a bit like typing "di*" to run the "dir". What if there is some other more dangerous command on the PATH that is going to match instead of "dir"?

Upvotes: 1

lit
lit

Reputation: 16236

I am not sure it would be a good idea to just run everything in a directory, but you could:

FOR %A IN ("*.jar") DO (java -jar "%~A")

Upvotes: 2

Related Questions