简体   繁体   中英

Running a java class that is located on a mapped network drive

I am trying to figure out whether it is possible to run a java class that is located on a mapped network drive. An example would be:

C:\temp\groovy>java p:\Test
Exception in thread "main" java.lang.NoClassDefFoundError: p:\Test
Caused by: java.lang.ClassNotFoundException: p:\Test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: p:\Test.  Program will exit.

Before trying this, I wanted to see whether I could run a java class file that is on UNC share (java \\\\somehost\\share\\Test ). This did not work either - same error about class def not being found.

Am I doing something wrong or is this really not supported ?

Thanks

When you give a path as part of the class name, Java expects it to be in a package corresponding to the folder hierarchy it's in.

Example:

> java z:/Test
java.lang.NoClassDefFoundError: z:/Test
Caused by: java.lang.ClassNotFoundException: z:.Test

Here it is looking for "z:.Test" as the fully qualified class name.

Assuming your Test class is not declared as being in any package, you need to specify the directory on the classpath:

java -cp Z:\path Test

The java launcher accepts a class as the first non-option argument for locating the main method that is to be invoked.

From the documentation :

By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used. If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header.

The Java runtime searches for the startup class, and other classes used, in three sets of locations: the bootstrap class path, the installed extensions, and the user class path.

You therefore, cannot provide p:\\Test as an argument. Instead change the current working directory to p:\\ , and execute java Test .

You may use a batch script to change the directory without performing this manually. Or you can package the class into a JAR file (with the required manifest), and provide the -jar option to specify the absolute path to the JAR file to the java executable; this is preferred if you do not want to manage too many class files.

Edit: You can also use the -cp flag as specified by @Dave Costa. This will enable you to do away with the need to change the current directory.

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