Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

java - Why is jar file not being found with classpath spec? (FileChooserDemo from Oracle)

Downloaded the latest Java SE. Ran FileChooserDemo via JNLP at http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html Works fine. (Windows box.)

Downloaded the source from http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#JWSFileChooserDemo

Following the compile instructions in the java source:

C:components> javac -classpath .;jars/jnlp.jar JWSFileChooserDemo.java 

This works fine. Two class files are generated in the components dir. Then...

C:components>cd .. 
C:src> java -classpath .;jars/jnlp.jar components.JWSFileChooserDemo 
Exception in thread "main" java.lang.NoClassDefFoundError:javax/jnlp/UnavailableServiceException 
at java.lang.Class.getDeclaredMethods0(Native Method) 
. 
. 
. 

So the UnavailableServiceException.class isn't being found. But if you list the jar file, the class IS there. So my classpath is just wrong.

C:jars> tar tf jnlp.jar
META-INF/
META-INF/MANIFEST.MF
javax/jnlp/
javax/jnlp/BasicService.class
javax/jnlp/ClipboardService.class
javax/jnlp/DownloadService.class
javax/jnlp/DownloadServiceListener.class
javax/jnlp/ExtensionInstallerService.class
javax/jnlp/FileContents.class
javax/jnlp/FileOpenService.class
javax/jnlp/FileSaveService.class
javax/jnlp/JNLPRandomAccessFile.class
javax/jnlp/PersistenceService.class
javax/jnlp/PrintService.class
javax/jnlp/ServiceManager.class
javax/jnlp/ServiceManagerStub.class
javax/jnlp/UnavailableServiceException.class

I tried this on Mac OSX (you have to change semicolons into colons in the classpath) and same exact thing.

Update: I found an older version of this demo online that doesn't use UnavailableSewrviceException and compiles standalone. It works fine with current Java and suits my purposes. I still do not understand why the commands given above do not work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Follow these steps :

Say the directory structure is this on my side, under C Drive :


  components-JWSFileChooserDemoProject
                  |
  ------------------------------------
  |           |           |          |
nbproject    src      build.xml    manifest.mf
              |
          components
              |
  -------------------------------------------------
  |       |       |                               |
images   jars     |                               |
             JWSFileChooserDemo.java      JWSFileChooserDemo.jnlp

Under components Directory create a new Directory called build so now Directory - components will have five thingies, instead of four i.e. build, images, jars, JWSFileChooserDemo.java and JWSFileChooserDemo.jnlp.

Now first go to components Directory.

To compile write this command :

C:components-JWSFileChooserDemoProjectsrccomponents>javac -classpath images*;jars*;build -d build JWSFileChooserDemo.java

Here inside -classpath option, you are specifying that content of Directories images, jars and build is to be included while compiling JWSFileChooserDemo.java. -d option basically tells, as to where to place the .class files.

Move to "build" folder :

C:components-JWSFileChooserDemoProjectsrccomponents>cd build

Run the Program :

C:components-JWSFileChooserDemoProjectsrccomponentsuild>java -cp .;..images*;..jars* components.JWSFileChooserDemo

Here inside -cp option . represents, that look from the current position, ..images* means, go one level up inside images Directory, from the current location and get all its contents and the same goes for ..jars* thingy too.

Now you will see it working, and giving the following output :

JWSFILECHOOSERDEMOIMAGE


EDIT 1 :

Since you wanted to do it without -d option of the Java Compiler - javac. Considering the same directory structure, as before, move inside your components Directory.

COMPILE with this command :

C:components-JWSFileChooserDemoProjectsrccomponents>javac -classpath images*;jars* JWSFileChooserDemo.java

Now manually create the package structure in the File System, i.e. create Directory components and then move your .class files created previously, inside this newly created components Directory, and also add images folder to this newly created components folder.

Now Directory - components will have five thingies, instead of four i.e. components(which further contains JWSFileChooserDemo.class , JWSFileChooserDemo$1.class and images folder), images, jars, JWSFileChooserDemo.java and JWSFileChooserDemo.jnlp.

RUN the program with this command :

C:components-JWSFileChooserDemoProjectsrccomponents>java -cp .;jars* components.JWSFileChooserDemo

This will give you the previous output, though, if you wanted to move as suggested before, again copy images folder to the automatically generated components folder, since I just looked inside the .java and they using relative path for it to work.


JUST THIS PARTICULAR SOLUTION, I AM ABOUT TO DESCRIBE WILL WORK FOR YOUR CASE ONLY :

If after javac command given before, if you don't wanted to create any folder, then go one level up, i.e. outside components directory and use this command to run the program

C:components-JWSFileChooserDemoProjectsrc>java -cp .;componentsjars* components.JWSFileChooserDemo

If you don't wanted to


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...