简体   繁体   English

创建一个bash别名以编译当前目录中的任何.java程序

[英]Create a bash alias to compile any .java program in the current directory

I am attempting to create a bash alias that compiles all files with the .java extension and then runs all the files with the .class extension in the current working directory. 我正在尝试创建一个bash别名,该别名将编译所有扩展名为.java的文件,然后在当前工作目录中运行所有扩展名为.class的文件。

I currently have the alias 我目前有别名

alias jcompile="cd $pwd; javac *.java; java *.class"

I would expect this to find any java programs and compile them, and then run them, but it throws this error. 我希望这能找到任何Java程序并进行编译,然后运行它们,但是会引发此错误。

error: file not found: *.java
Usage: javac <options> <source files>
use --help for a list of possible options
Error: Could not find or load main class *.class
Caused by: java.lang.ClassNotFoundException: *.class

Unless you're defining $pwd specifically, it doesn't mean anything. 除非您专门定义$pwd ,否则它没有任何意义。 pwd is a command that will tell you your current path, and $PWD is a variable that holds your current path. pwd是将告诉您当前路径的命令,而$PWD是保存您当前路径的变量。 If you are defining a variable pwd , you might want to change it to something more distinctive. 如果要定义变量pwd ,则可能需要将其更改为更具特色的内容。

If you want your function to run in a location that isn't your current directory, you could pass it as an argument: 如果您希望函数在当前目录之外的位置运行,则可以将其作为参数传递:

jcompile() {
  cd "$1"
  javac *.java
  java *.class
}

and run it via 并通过运行

jcompile /my/path/to/directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM