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

后端 未结 1 459
清歌不尽
清歌不尽 2020-12-20 02:15

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

1条回答
  •  囚心锁ツ
    2020-12-20 02:49

    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-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. -d option basically tells, as to where to place the .class files.

    Move to "build" folder :

    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 . 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-JWSFileChooserDemoProject\src\components>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-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.


    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-JWSFileChooserDemoProject\src>java -cp .;components\jars\* components.JWSFileChooserDemo
    

    If you don't wanted to

    0 讨论(0)
提交回复
热议问题