java - 用于编译和运行java东西的.bat文件

标签 java batch-file cmd

我正在尝试创建两个bat 文件。一个是c.bat,另一个是r.bat

c.bat 仅包含:

cls
javac *.java

它将编译所有 .java 文件。

在 r.bat 中我有:

cls
java *

它不会运行类文件。我认为这是因为“java *”行中的 * 翻译为“java Class1.class”、“java Class2.class”等,而它应该只是“java Class1”和“java Class2”。 (没有扩展)我该怎么做?我刚刚开始学习这些东西,但在任何地方都找不到正确的答案。

最佳答案

以下循环遍历在 C:\java\stuff 中找到的所有 .java 文件,并依次运行它们。 %%~nf 正在格式化文件名,以不显示文件扩展名即 java 类名。如果将 java %%~nf 更改为 echo java %%~nf 您可以准确地看到发生了什么。

cls
for "C:\java\stuff" %%f in (*.java) do (
    java %%~nf
)

For :以下选项可用:

Variable with modifier  Description

%~I                     Expands %I which removes any surrounding 
                        quotation marks ("").
%~fI                    Expands %I to a fully qualified path name.
%~dI                    Expands %I to a drive letter only.
%~pI                    Expands %I to a path only.
%~nI                    Expands %I to a file name only.
%~xI                    Expands %I to a file extension only.
%~sI                    Expands path to contain short names only.
%~aI                    Expands %I to the file attributes of file.
%~tI                    Expands %I to the date and time of file.
%~zI                    Expands %I to the size of file.
%~$PATH:I               Searches the directories listed in the PATH environment 
                        variable and expands %I to the fully qualified name of 
                        the first one found. If the environment variable name is 
                        not defined or the file is not found by the search,
                        this modifier expands to the empty string. 

关于java - 用于编译和运行java东西的.bat文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13090609/

相关文章:

node.js - npm包如何生成命令行文件

java控制台输出成为下一个控制台输入

Java初学者在这里获得非零数字

windows - 批处理文件变量文件名和编辑

batch-file - 将文件从远程服务器移动到另一台远程服务器

excel - 如何从命令行启动 Excel 宏(没有 Worksheet_Open 事件)?

batch-file - 如何从批处理文件更改字体大小?

java - 在 Akka Scheduler 中的特定日期安排作业

java - 我们如何使用可变引用来保持类的不可变性

windows - 如何根据文件大小比较复制文件?