2

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 
4
  • "... but its not working". What does it say? Commented Aug 7, 2020 at 10:16
  • @StephenC,its showing error Error: Unable to access jarfile *.jar Commented Aug 7, 2020 at 10:17
  • Does it work if you execute it without using a * ? Commented Aug 7, 2020 at 10:22
  • nope,It is only executing if am giving full name of jar like "java -jar abc.jar" Commented Aug 7, 2020 at 10:23

2 Answers 2

2

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") 
Sign up to request clarification or add additional context in comments.

Comments

1

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"?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.