Calling C from Java8 using JNA via cygwin - java

Reproducing the instructions at
http://stuf.ro/calling-c-code-from-java-using-jna
for calling C code from java I have:
/* ctest.c */
#include <stdio.h>
void helloFromC()
{
printf("Hello from C!\n");
}
This is compiled into the library using:
gcc -o libctest.so -shared ctest.c
Then Java calling program is:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class HelloWorld
{
public interface CTest extends Library
{
public void helloFromC();
}
public static void main(String argv[])
{
CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
ctest.helloFromC();
}
}
Then the java is compiled in Java8 using the -cp classpath argument:
javac HelloWorld.java -cp jna.jar
This step works, producing two class files, HelloWorld.class and HelloWorld$1.class.
The execute step does not work:
java HelloWorld -cp jna.jar
instead producing the following output:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Library
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at HelloWorld.main(HelloWorld.java:14)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
The output is effectively identical to: JNA example program java.lang.NoClassDefFoundError which I have carefully reviewed.
I have run many tests of java8 under cygwin and they all work, this one does not.
I have constructed many permutations of this example, including creating the sun.com.jna directories and including HelloWorld in the package. I have also used different delimiters which are necessary in some circumstances. All of them fail. What am I missing?

It seems that you didn't set your classpath. NoClassDefFoundErrorindicates that your ClassLoadercan't find your compiled classes.

Related

Running a program after compiling it with javac

I have a source file named Plane.java. I am trying to compile and run the program in terminal, but i can't!
I am compiling it with:
javac Plane.java
So, I am getting with ls my classes
CargoBay.class
CleaningEmployee.class
Employee.class
EquipmentComponent.class
MaintenanceEmployee.class
PassengerComponent.class
Plane.class
Plane.java
PlaneComponent.class
PrivateComponent.class
SecurityEmployee.class
Now, I have no idea how to run it! I am trying with java Plane, but I keep getting errors. Any ideas ?
(errors: after java Plane)
Exception in thread "main" java.lang.NoClassDefFoundError: Plane (wrong name: plane/Plane)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The program runs just fine in netbeans
Compile your class by
javac -d . Plane.java
This will create a plane directory at current location and put your classes into that directory then run your program using
java plane.Plane
Your classes are probably in a package named plane. The compiler creates a directory with the package name. What you have to do is go up one level from there
and call:
java plane.Plane
Other options would be to set the CLASSPATH environment variable or use the -cp option of the java command.

How to use classpath in ubuntu terminal for executing java class?

I tried to execute CreateTextFileTest.class file with classpath in the terminal as the following:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile" CreateTextFileTest
My class files in this directory : /home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17/unit17/creatingtextfile .
I have 3 classes in the directory: AccountRecord.class , CreateTextFile.class, and CreateTextFileTest.class
But, when I executed the CreateTextFileTest from the terminal with the code above, an error occured like that:
Exception in thread "main" java.lang.NoClassDefFoundError: CreateTextFileTest (**wrong name**: unit17/creatingtextfile/CreateTextFileTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
As far as I understand because CreateTextFileTest.java uses two different classes, executing fails. How to handle this situation? How to run my java project? How to use classpath in this situation? Am I using wrongly?
The error message states that the package name declared in your compiled class doesn't match what the JVM was expecting given your classpath setup. The classpath should point to the directory from which your packages start, not the directory actually containing your .class files. Given your error message I believe that this should work:
java -classpath ..:"/home/fatih/NetBeansProjects/Unit17 - CreatingTextFile/src/unit17" unit17.creatingtextfile.CreateTextFileTest
Given the duplicate unit17 in your path I get the impression there's something else mixed up here. You'll get a better answer on Stack Overflow...

classpath error in java

I have written a java program where I am using a .jar library (Jinterface). I compile it using:
javac -classpath lib/OtpErlang.jar Game.java Actions.java
where the lib dir is inside my project folder.
There is no error when compiling but I get a runtime error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: com/ericsson/otp/erlang/OtpErlangObject
at Game.main(Game.java:7)
Caused by: java.lang.ClassNotFoundException: com.ericsson.otp.erlang.OtpErlangObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
And I think that it has to do with the classpath, what is wrong?
Here is the Game class:
import java.io.IOException;
public class Game {
public static void main(String[] args) throws IOException {
Actions server = new Actions();
server.moveRight();
}
}
Try running it like this, from the root of all your java packages:
java -classpath lib/OtpErlang.jar Game
This command did the trick:
java -classpath lib/OtpErlang.jar:. Game
I have missed the :. (which tells where my main class is, I suppose...)
Try like this.I hope this will help.....
javac -cp . lib/OtpErlang.jar Game.java Actions.java

I can't import fixture into my fitnesse page

I wanted to begin test my Java Apps with Fitnesse, but I have a big problems right at beginning.
My fitnesse page to test my very simple class is:
!contents -R2 -g -p -f -h
!define TEST_SYSTEM {slim}
!path /home/user/NetBeansProjects/dotestow/build/classes/dotestow/
!|dodawanie|
|l1|l2|add?|
|10|2|12|
|10|35|45|
|60|4|33|
My class is just normal Java class created by NetBeans, here is the code:
package dotestow;
public class Dotestow {
private int l1, l2;
public void setL1(int l1) {this.l1 = l1;}
public void setL2(int l2) {this.l2 = l2;}
public int add()
{
return l1+l2;
}
}
When I click test, it just shows:
Exception in thread "main" java.lang.NoClassDefFoundError: fitnesse/slim/SlimService
Caused by: java.lang.ClassNotFoundException: fitnesse.slim.SlimService
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: fitnesse.slim.SlimService. Program will exit.
What am I doing wrong??
##EDIT
When I run
java Dotestow
in my /Dotestow/build/classes directory, i got output:
Exception in thread "main" java.lang.NoClassDefFoundError: Dotestow (wrong name: dotestow/Dotestow)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Dotestow. Program will exit.
Answer Updated
Looks like FitNesse.jar is missing from the classpath or JRE missmatch with the FitNesse jar version. Please check if Fitnesse.jar is added to classpath in netbeans.
Regards,
Explicitly add fitnesse.jar to your classpath using !path, giving the full absolute path to the .jar
Change this
!path /home/user/NetBeansProjects/dotestow/build/classes/dotestow/
for this
!path /home/user/NetBeansProjects/dotestow/build/classes/
|Import|
|dotestow|
or
remove (from your Class)
package dotestow;
and use javac to complile :D

NoClassDefFoundError using Scala class from Java

I have no experience with Scala, so this question may be elementary. I am trying to use Scala class from within Java, based on the "Person" example in this tutorial: http://www.codecommit.com/blog/java/interop-between-java-and-scala
I create two source files, one Scala and one Java, as follows.
Person.scala:
class Person {
def getName() = "Daniel Spiewak"
}
Test.java:
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.getName();
}
}
I can compile (with a warning I don't understand), but I get a ClassNotFoundException when I try to run the program.
$ scalac Person.scala
$ javac Test.java
./Person.class: warning: Cannot find annotation method 'bytes()' in type 'ScalaSignature': class file for scala.reflect.ScalaSignature not found
1 warning
$ java Test
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at Test.main(Test.java:3)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 13 more
Any idea what I'm doing wrong?
You need to include the Scala runtime library to be able to use Scala classes. This is the cause of both the compiler warning and the error at runtime.
Try this
java -cp .:scala-library.jar Test
(the name of the jar file might be different, but the point is to add it to your classpath).
Did you put scala-library.jar in your classpath? Seems you forgot this, so JVM cannot find some internal class for Scala runtime.
In your link http://www.codecommit.com/blog/java/interop-between-java-and-scala:
Just stick scala-library.jar on your classpath and all of your Scala
classes should be readily available within your Java application.

Categories

Resources