0

I've been trying to create a .h file with the command :

javah -jni MyClass

of course after doing

javac MyClass.java

which doesn't give me any error messages...

However whenever I execute the javah -jni command I get the following error:

Exception in thread "main" java.io.IOException: can't find class file
MyClass.class in 
java.net.URLClassLoader{urls=[file:/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre/lib/rt.jar], 
parent=gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}}
               at gnu.classpath.tools.javah.Main.getClass(libgcj-tools.so.14)
               at gnu.classpath.tools.javah.Main.parseClasses(libgcj-tools.so.14)
               at gnu.classpath.tools.javah.Main.run(libgcj-tools.so.14)
               at gnu.classpath.tools.javah.Main.main(libgcj-tools.so.14)

By the way I'm working on a Windows 10 PC

AlexT
  • 589
  • 2
  • 9
  • 23
  • 1. What `javah` command line are you using? 2. Is your class in a package? 3. Why are you using GNU CLASSPATH? It's as dead as a doornail. Let it rest in peace. Use a JDK. – user207421 Apr 30 '17 at 20:42
  • 1. I just type `javah -jni MyClass directly after compiling MyClass.java 2. My class in not in a package 3. I wasn't even aware that I was using GNU CLASSPATH, please enlighten me – AlexT Apr 30 '17 at 21:00
  • Take a look at the class names in the stack trace. GNU CLASSPATH was a failed and indeed doomed attempt on the part of FSF to provide an open-source Java. They never even finished 1.2 but it is still around in Linux distributions for some reason, twenty years later. Remove it and install a JDK. Why is the class name in the stack trace different from the one in the command line? Have you compiled it? – user207421 Apr 30 '17 at 21:07
  • About the different class name in the stack trace, I just copied the wrong error. I have indeed compiled it... Do you mind explaining how to remove the GNU CLASSPATH? Please forgive me I am a complete beginner at this – AlexT Apr 30 '17 at 21:24
  • I have no idea. Something to do with RPM I would guess, but don't quote me. Is the .class file in the current directory when you run `javah`? – user207421 Apr 30 '17 at 21:37
  • @EJP Yes, the .class file is in the current file – AlexT Apr 30 '17 at 21:46
  • Spelling the file name correctly? Including case? Filename agrees with internal class name? – user207421 Apr 30 '17 at 22:05
  • @EJP Yes, yes, and yes! – AlexT Apr 30 '17 at 22:58
  • Well try it with a JDK. – user207421 Apr 30 '17 at 23:26

1 Answers1

0

Take a look at package defined inside MyClass. It might be that you are using some package name and then you are passing just class name - this will not work.

Make sure that class is available via CLASSPATH.

Alternatively, you can pass class location (together with package) via -cp argument for javah.

Take a look here:

http://jnicookbook.owsiak.org/recipe-No-001/

You can find there fully working, supper simple sample.

It should work out of the box. Just do:

git clone https://github.com/mkowsiak/jnicookbook.git
cd jnicookbook/recipes/recipeNo001
make; make test

If you still have issues here. It might be there is something not quite ok with your installation.

If it works for you, just make sure to use similar compilation flags as you can find inside Makefile

Have fun with JNI :)

Update:

As already pointed out in comments above, maybe you can switch to Oracle's JDK?

If you use Oracle's JDK there are no issues of that sort. You can easily generate C header using javah

In case you have to stick to GCJ (e.g. legacy code), maybe give it a try with

javah -jni -cp . Main
Oo.oO
  • 12,464
  • 3
  • 23
  • 45