8

Environment: Windows 7, Java 6.

Trying to compile a .java file with -cp option. The file uses a single jar file that's in the current directory ALONG WITH some other jar files in the current directory.

javac -cp ./*.jar MyFile.java

doesn't work.

javac -cp ./* MyFile.java 

doesn't work

javac -cp ./MyJar.jar MyFile.java

works

First two cases, I get a invalid flag error. Can someone explain this behavior?

And I checked if it is spaces issue, there are no spaces anywhere in my full file paths.

Brian
  • 1,337
  • 5
  • 17
  • 34

2 Answers2

14

The quoted sources for the two links provided in the comments as well as in the "This question may already have an answer here:", do not completely explain the observed behavior.

javac -cp ./*.jar MyFile.java

Won't work, because the wildcard * usage in this context differs from normal usage. This can be understood from the documentation. * always represents full file(s) and not partial file names.

javac -cp ./* MyFile.java

Should have worked. Apparently using double quotes and/or a semi-colon in windows. works:

javac -cp "./*" MyFile.java

javac -cp ./*; MyFile.java

javac -cp "./*;" MyFile.java

javac -cp *; MyFile.java

javac -cp "*" MyFile.java

javac -cp "*;" MyFile.java

Nowhere in the documention is this important fact mentioned afaik.

So I guess ON WINDOWS 7 64 bit, with java 1.6.0_75 EITHER USE DOUBLE QUOTES OR ALWAYS A SEMI-COLON WHEN USING WILDCARD *

Community
  • 1
  • 1
Brian
  • 1,337
  • 5
  • 17
  • 34
  • I didnt have to use quotes I just used C:\share\NetBeansProjects\JLibUser>%JAVA_HOME%\bin\javac src\jlibuser\\*.java -cp ..\JLib\dist\\* – Kalpesh Soni Jul 29 '14 at 21:43
  • @KalpeshSoni could be to do with using current directory? – Brian Jul 29 '14 at 21:52
  • @KalpeshSoni Couldn't reproduce your behavior. I moved all my jars from current dir to a child dir in it, and still above behavior holds. I need to do either -cp libs/*; or -cp "libs/*" – Brian Jul 29 '14 at 21:58
  • Mine is 1.6.0_75-b13 on windows 7 64 bit – Brian Jul 30 '14 at 13:34
  • I use `cygwin 2.8`, win 7, and only double quotes `and` semicolon work in case of multiple `jars`. – Timo Nov 02 '17 at 11:19
0

use backslash in windows?

try

javac -cp .\* MyFile.java

also note Broken wildcard expansion for Java7 commandline on Windows(7?)

Community
  • 1
  • 1
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59