简体   繁体   中英

Java compiler does not see packages if jar on classpath

Why doesn't following command work?

$ javac -encoding UTF8 -classpath ./piccolo-1.2.jar:./piccolox-1.2.jar com/google/scrollview/ui/SVAbstractMenuItem.java

It produces multiple errors like

com\google\scrollview\ui\SVAbstractMenuItem.java:22: package com.google.scrollview.events does not exist
import com.google.scrollview.events.SVEventType;
                                   ^

This is real code in file SVAbstractMenuItem.java line 22, but warning statement is wrong, because package exists:

$ ls com/google/scrollview/events/
Makefile  Makefile.am  Makefile.in  SVEvent.java  SVEventHandler.java  SVEventType.java

How to teach this compiler to see packages?

UPDATE

Code is not mine, this is the makefile from tesseract and I am trying to run it under cygwin. Everything looks correct.

Including current directory gives the same result:

$ javac -encoding UTF8 -classpath ./piccolo-1.2.jar:./piccolox-1.2.jar:. com/google/scrollview/ui/SVAbstractMenuItem.java
com\google\scrollview\ui\SVAbstractMenuItem.java:22: package com.google.scrollview.events does not exist
import com.google.scrollview.events.SVEventType;
                                   ^
com\google\scrollview\ui\SVAbstractMenuItem.java:56: cannot find symbol
symbol  : class SVWindow
location: class com.google.scrollview.ui.SVAbstractMenuItem
  public void performAction(SVWindow window, SVEventType eventType) {}
                            ^
com\google\scrollview\ui\SVAbstractMenuItem.java:56: cannot find symbol
symbol  : class SVEventType
location: class com.google.scrollview.ui.SVAbstractMenuItem
  public void performAction(SVWindow window, SVEventType eventType) {}
                                             ^
3 errors

UPDATE 2

I found this occur if JAR present in classpath parameter.

UPDATE 3

I realized that if a colon would separate paths, then Windows/DOS drive letter can't handled. Hence, separating with colon is wrong.

SOLVED

This was Cygwin/Java incompatibility. Windows' Java uses semicolon to separate paths, while linux and makefiles expexts linux-style, ie colon. So it was not working under cygwin. Even replacing colon to semicolon in makefile wasn't solving the problem.

Only extracting all jars content into single plain directory helped.

Using javac in cygwin can be difficult. Java compiler is not a cygwin but a windows program. So you must use Windows-style paths.

Because you use the colon ":" in your classpath, your classpath cannot be correctly interpreted by the Windows Java compiler. You would have to use semicolon ";" as path seperator in your classpath. But because you are in a Unix-style shell ";" terminates your command.

Either compile your java sources without cygwin. Since the result are java class files it does not matter. Or put your parameters like classpath and also path to source files in single quotes.

Example:

javac -encoding UTF8 -classpath '.\piccolo-1.2.jar;.\piccolox-1.2.jar;.' 'com\google\scrollview\ui\SVAbstractMenuItem.java'

See also this post: setting multiple jar files as classpath in cygwin

You forgot the current directory "." in your classpath, so javac isn't even searching the correct path.

Include it in -classpath :

$ javac -encoding UTF8 -classpath ./piccolo-1.2.jar:./piccolox-1.2.jar:. com/google/scrollview/ui/SVAbstractMenuItem.java

经过多次试验,我发现行之有效的是仅在路径开头使用了转义的反斜杠,并使用分号作为分隔符。

export CLASSPATH=C:\\Users/username/bla/bla.jar;C:\\Users/username/bla/bla.jar 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM