简体   繁体   English

为什么在classpath规范中找不到jar文件? (来自Oracle的FileChooserDemo)

[英]Why is jar file not being found with classpath spec? (FileChooserDemo from Oracle)

Downloaded the latest Java SE. 下载了最新的Java SE。 Ran FileChooserDemo via JNLP at http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html Works fine. 通过JNLP在http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html上运行 FileChooserDemo。 (Windows box.) (Windows框。)

Downloaded the source from http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#JWSFileChooserDemo http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#JWSFileChooserDemo下载了源代码

Following the compile instructions in the java source: 按照java源代码中的编译说明进行操作:

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. 因此UnavailableServiceException.class找到UnavailableServiceException.class But if you list the jar file, the class IS there. 但是,如果您列出了jar文件,则类就在那里。 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. 我在Mac OSX上尝试过此操作(您必须将分号更改为类路径中的冒号),并且完全相同。

Update: I found an older version of this demo online that doesn't use UnavailableSewrviceException and compiles standalone. 更新:我在网上找到了此演示的较旧版本,该版本不使用UnavailableSewrviceException并独立编译。 It works fine with current Java and suits my purposes. 它可以在当前的Java下正常工作,并且适合我的目的。 I still do not understand why the commands given above do not work. 我仍然不明白为什么上面给出的命令不起作用。

Follow these steps : 跟着这些步骤 :

Say the directory structure is this on my side, under C Drive : 说目录结构在我这边,在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 ie build , images , jars , JWSFileChooserDemo.java and JWSFileChooserDemo.jnlp . components Directory创建一个名为build的新Directory ,现在Directory - components将具有五个东西,而不是四个,即buildimagesjarsJWSFileChooserDemo.javaJWSFileChooserDemo.jnlp

Now first go to components Directory . 现在首先转到components Directory

To compile write this command : 要编译,请编写以下命令:

C:\components-JWSFileChooserDemoProject\src\components>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 . -classpath选项中,您指定在编译JWSFileChooserDemo.java要包含Directories imagesjarsbuild JWSFileChooserDemo.java -d option basically tells, as to where to place the .class files. -d选项基本上告诉您.class文件的放置位置。

Move to "build" folder : 移至"build"文件夹:

C:\components-JWSFileChooserDemoProject\src\components>cd build

Run the Program : 运行程序:

C:\components-JWSFileChooserDemoProject\src\components\build>java -cp .;..\images\*;..\jars\* components.JWSFileChooserDemo

Here inside -cp option . 在这里-cp选项. 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. 表示,从当前位置看, ..\\images\\*意味着从当前位置在images Directory一层,并获取其所有内容, ..\\jars\\*也是一样。

Now you will see it working, and giving the following output : 现在您将看到它正在工作,并提供以下输出:

JWSFILECHOOSERDEMOIMAGE


EDIT 1 : 编辑1:

Since you wanted to do it without -d option of the Java Compiler - javac . 由于您想在没有Java Compiler - javac -d选项的情况下进行操作。 Considering the same directory structure, as before, move inside your components Directory . 考虑到与以前相同的目录结构,请在components Directory移动。

COMPILE with this command : 使用以下命令进行编译:

C:\components-JWSFileChooserDemoProject\src\components>javac -classpath images\*;jars\* JWSFileChooserDemo.java

Now manually create the package structure in the File System , ie 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. 现在,在File System手动创建package structure ,即创建Directory components ,然后将先前创建的.class文件移动到此新创建的components Directory ,并将images文件夹添加到此新创建的components文件夹中。

Now Directory - components will have five thingies, instead of four ie components(which further contains JWSFileChooserDemo.class , JWSFileChooserDemo$1.class and images folder) , images , jars , JWSFileChooserDemo.java and JWSFileChooserDemo.jnlp . 现在, Directory - components将有五个东西,而不是四个components(which further contains JWSFileChooserDemo.class , JWSFileChooserDemo$1.class and images folder)imagesjarsJWSFileChooserDemo.javaJWSFileChooserDemo.jnlp

RUN the program with this command : 使用以下命令运行程序:

C:\components-JWSFileChooserDemoProject\src\components>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. 但是,如果您要按照之前的建议进行移动,这将为您提供先前的输出,再次将images文件夹复制到自动生成的components文件夹中,因为我只是在.java内部浏览,并且它们使用relative path来工作。


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, ie outside components directory and use this command to run the program 如果在之前给出了javac命令之后,如果您不想创建任何文件夹,请向上一级,即在components目录之外,并使用此命令运行程序

C:\components-JWSFileChooserDemoProject\src>java -cp .;components\jars\* components.JWSFileChooserDemo

If you don't wanted to 如果你不想

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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