views:

1902

answers:

2

I can compile this JNA example code (from step 2 of https://jna.dev.java.net/#getting_started):

package com.sun.jna.examples;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {

    // This is the standard, stable way of mapping, which supports extensive
    // customization and mapping of Java to native types.
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)
            Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
                               CLibrary.class);

        void printf(String format, Object... args);
    }

    public static void main(String[] args) {
        CLibrary.INSTANCE.printf("Hello, World\n");
        for (int i=0;i < args.length;i++) {
            CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
        }
    }
}

...using javac -classpath .:jna.jar -g HelloWorld.java without error. (I downloaded jna.jar and put it in the same directory as HelloWorld.java for now.)

But when I run it using java -classpath .:jna.jar HelloWorld, I get:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: com/sun/jna/examples/HelloWorld)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

I get the exact same exception on Mac OS X and Linux.

How do I get this to run?

+3  A: 

This example (as well as vast majority of java classes) uses packages:

package com.sun.jna.examples;

In order to compile / run it properly you need to run javac / java from the "root" folder (e.g. folder where "com" is located):

Let's say you have a folder called examples. You'd put both the jna.jar and the source code in it preserving folder structure:

/examples
 jna.jar
 /com
   /sun
      /jna
         /examples
           HelloWorld.java

You compile and run using:

javac -classpath .:jna.jar -g com/sun/jna/examples/HelloWorld.java

java -classpath .:jna.jar com.sun.jna.examples.HelloWorld

Note the path separators in the former case and dots in the latter.

ChssPly76
When compiling, you want to also include the -d . flag as well to make sure that it blows out the directory structure base on the package. Otherwise the class file will just be in the current directory.
Rob Di Marco
Rob, wouldn't adding "-d ." when compiling put the class file in the current directory. It appears to default to putting it with the .java source file. Why wouldn't I want to do that?
Daryl Spitzer
@Rob - "-d" flag does specify target folder but it has nothing to do with "blowing out directory structure based on package". The latter is **always** the case.
ChssPly76
@Daryl - you may not want to do that in a real project; keeping your source separately from build artifacts is a good idea for many reasons (like making it a lot easier to check your sources into VCS). For your purposes, though (running an example), it's totally fine as is.
ChssPly76
Daryl Spitzer
@Daryl - as they should be. Specifying "-d ." is the same as not specifying it at all. If you want to try it out, create another folder called "build" (under "/examples") and specify "-d build" as `javac` option. Be aware that you'd have to add it to classpath for `java` invocation afterwards: `java -classpath build:jna.jar com.sun.jna.examples.HelloWorld`
ChssPly76
+2  A: 

Either just remove this line and recompile (which is fine in this case as you just try out some sample)

package com.sun.jna.examples;

or read up on what packages in java are and how they have to be handled (ChssPly76s Posts as a starter).

Better choose the second option as sooner or later (probably sooner) you will have to deal with packages anyway. So just take the time now to read up on it.

jitter