Terence Lyu
Terence Lyu

Reputation: 329

Invalid flag error when using bash to compile java

I am trying to write a simple script to compile my java code. When I put

javac Main.java

in my compile.sh and do

bash compile.sh

I get error: invalid flag: Main.java

However, if I just simply use the command javac Main.java , everything works fine.

I am using a Ubuntu VirtualBox on Win10. How can I get my script working?

Upvotes: 0

Views: 2015

Answers (1)

that other guy
that other guy

Reputation: 123480

Here's the problem you're seeing:

$ cat compile.sh
javac Main.java

$ bash compile.sh
javac: invalid flag: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options

This happens because the script uses DOS style line endings:

$ cat -v compile.sh
javac Main.java^M

You can fix it by setting your editor to save it with Unix line terminators, or with e.g. tr:

$ tr -d '\r' < compile.sh > fixed.sh
(no output)

$ cat -v fixed.sh
javac Main.java

It now works as expected:

$ bash fixed.sh
(no output)

Upvotes: 4

Related Questions