Search code examples
bashshelljavac

How can I fix a JAVAC Invalid flag error?


I'm trying to compile a shell script with javac and it keeps returning me this error:

$ bash run_RGE.sh

javac: invalid flag: Element.java
Usage: javac <options> <source files>
use -help for a list of possible options
javac: invalid flag: RGE.java
Usage: javac <options> <source files>
use -help for a list of possible options
javac: invalid flag: -Xms1024m
Usage: javac <options> <source files>
use -help for a list of possible options
sleep: missing operand
Try "sleep --help" for more information.

My shell script looks like this :

#!/bin/bash
# -*- coding: utf-8 -*-
export LIB=C:/home/b04280/Transfert/Libs/
CLASSPATH=$(echo "$LIB"/*.jar | tr ' ' ':')
export ficIn=C:/home/b04280/Transfert/rgeII.xlsx/
export repIn=C:/home/b04280/Transfert/rgeII/
export repOut=C:/home/b04280/Transfert/rgeIIcible/
javac -cp $CLASSPATH Element.java
javac -cp $CLASSPATH RGE.java
javac -Xms1024m -Xmx1024m -cp $CLASSPATH  RGE $ficIn $repIn $repOut > docx.txt
sleep 

I am wondering where this error comes from, I looked at other questions on here regarding the same topic but their solutions didn't seem fit to my particular case.

Any help please? Thanks in advance.

PS. I also have the sleep error (I "translated" this script from batch --> shell and "sleep" was the replacement I found for "pause")


Solution

  • I believe this should be closer to what you need :

    #!/bin/bash
    # -*- coding: utf-8 -*-
    lib=/home/b04280/Transfert/Libs
    classpath=$(echo "$lib"/*.jar | tr ' ' ':')
    fic_in=/home/b04280/Transfert/rgeII.xlsx/
    rep_in=/home/b04280/Transfert/rgeII/
    rep_out=/home/b04280/Transfert/rgeIIcible/
    javac -cp "$classpath" Element.java
    javac -cp "$classpath" RGE.java
    java -Xms1024m -Xmx1024m -cp "$classpath" RGE "$fic_in" "$rep_in" "$rep_out" > docx.txt
    

    In particular :

    • unix paths
    • quoted variables to avoid them being parsed as multiple arguments if their values contain spaces
    • in the last command RGE is a class name rather than a file path, you specify memory settings, plus the command produces useful output ; it seems to me it should execute the code (java) rather than compile it (javac)
    • I'm not sure it's necessary, but removing the trailing slash from $LIB's value avoids having double slashes in $CLASSPATH which might confuse java[c]
    • removed the useless exports (they'd be useful if you invoked other bash scripts or binaries that would rely on those variables being set, but you're just passing them as parameters)
    • applied naming conventions (avoid uppercase variables as they might conflict with environment variables, use snake_case rather than camelCase).

    I'm not sure in your context whether you're executing the linux version of the java[c] binaries or the windows one. If it's the windows one you'll need to replace the tr ' ' ':' part by tr ' ' ';' as the classpath on windows is semi-colon separated rather than colon-separated.