0

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.

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.

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
3
  • What contains $pwd? Commented Jul 2, 2019 at 17:40
  • i was trying to get the working directory, i guess it was supposed to be $PWD Commented Jul 2, 2019 at 17:45
  • 2
    cd $(pwd) or better cd "$PWD". But both are useless. Commented Jul 2, 2019 at 17:47

1 Answer 1

3

Unless you're defining $pwd specifically, it doesn't mean anything. pwd is a command that will tell you your current path, and $PWD is a variable that holds your current path. If you are defining a variable pwd, you might want to change it to something more distinctive.

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

2 Comments

i was using pwd to get the working directory, not defining a new one
then just remove that part of the alias, and it should work. You don't need to cd to your current location, you're already there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.