Search code examples
bashcommand-linejavac

Javac giving an error while trying to compile all java files in a folder using linux script


I have a script which has the following

#!/bin/bash
javac -classpath /netweka/weka-3-6-10/weka.jar ./*.java

when I try to run this script on a Linux terminal, I get an error saying :

: not found
javac: invalid flag:
Usage: javac  
use -help for a list of possible options
: not found*.java

I want to compile all the java files in the current directory. What am I missing ?

Thank you in advance.


Solution

  • I think you created that script file in Windows, and it has Windows (cr-lf) line endings. Those won't work with Linux utilities, because the CR (0x0D, control-M, or \r) is considered a regular character. In this case, it's become part of the glob ./*.java\r, which therefore (a) matches nothing, and (b) inserts a carriage return in the middle of the error message making it hard to read.

    Convert your script to Linux line endings, using a Linux text editor, dos2unix or tr -d $'\r' < old > new where old and new are the input and output files.