如何在终端中执行 .jar 文件而不指定其名称。

huangapple go评论70阅读模式
英文:

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

问题

我可以执行文件夹中的 .jar 文件,而不需要在 Windows 命令提示符中指定文件名吗?

我已尝试以下命令(因为该文件夹中只有一个 .jar 文件,所以我使用了 * ),但它不起作用(错误:无法访问 jar 文件 *.jar )。

java -jar *.jar
英文:

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

答案1

得分: 2

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

英文:

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

答案2

得分: 1

你现在所询问的是如何运行以下命令:

% java -jar somelongname.jar

像这样运行:

% java -jar *.jar

以避免输入一些字符。这是不可能的,因为无论是Windows CMD shell还是java命令都不会展开*.jar通配符模式。

(在Linux / Unix / MacOS上,shell在将参数传递给命令之前会展开通配符。在Windows上,展开通配符的责任由命令来承担。实际上,看起来java命令只会展开要传递给您的应用程序的参数中的通配符;请参阅https://stackoverflow.com/questions/25948706/stop-expanding-wildcard-symbols-in-command-line-arguments-to-java。)

所以,如果你想在Windows上避免输入这些繁琐的字符,你需要做一些像这样的事情:

  • 编写一个简单的BAT文件来运行"java -jar somelongname.jar",或者
  • 编写一个聪明的BAT文件来识别并运行与"*.jar"匹配的JAR文件,或者
  • 使用Powershell。

不管价值多少,我认为你正在尝试做的事情相当危险。这有点像输入"di*"来运行"dir"。如果PATH上有一些其他更危险的命令匹配而不是"dir"会怎么样呢?

英文:

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 https://stackoverflow.com/questions/25948706/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"?

huangapple
  • 本文由 发表于 2020年8月7日 18:01:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63299498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定