Results 1 to 16 of 16

Thread: JNI Help

  1. #1
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default JNI Help

    I an trying to import some native code into my java program to enable changing the color of the console and wiping it,but for some reason it fails when generating the c++ header file please watch the video and tell me if you see what i am doing wrong



  2. #2
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    if no one can figure out why this doesnt work can someone javah this for me and post the header file

    Java Code:
    public class Console
    {
    	static
    	{
    		System.loadLibrary("console");
    	}
    	
    	/**
    	 * Wipes all text off the console screen
    	 */
    	public native static void clearScreen();
    	
    	private native static void setColor(char background, char foreground);
    	
    	/**
    	 * Sets the color of the console in hex the java program is running on may not work on all consoles
    	 * @param background color
    	 * @param foreground color
    	 */
    	public static void setColor(byte background, byte foreground)
    	{
    		String background2 = Integer.toHexString(background);
    		String foreground2 = Integer.toHexString(foreground);
    		char f=foreground2.charAt(0);
    		char b=background2.charAt(0);
    		setColor(b,f);
    	}
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JNI Help

    I just did a manual javah run and got this result:

    yr12_m09_b_Console.h
    Java Code:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class yr12_m09_b_Console */
    
    #ifndef _Included_yr12_m09_b_Console
    #define _Included_yr12_m09_b_Console
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     yr12_m09_b_Console
     * Method:    clearScreen
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_yr12_m09_b_Console_clearScreen
      (JNIEnv *, jclass);
    
    /*
     * Class:     yr12_m09_b_Console
     * Method:    setColor
     * Signature: (CC)V
     */
    JNIEXPORT void JNICALL Java_yr12_m09_b_Console_setColor
      (JNIEnv *, jclass, jchar, jchar);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    Note though that my Console class uses this package:

    package yr12.m09.b;

    and the package name will be part of the java name in the header file above. above.

  4. #4
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    im confused about what i have to do now that you have put it in a package named yr12_m09_b

  5. #5
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    i hate to bother people but can someone javah this for me it still wont work

    Java Code:
    public class Console
    {
    	private Console()
    	{
    		
    	}
    	
    	static
    	{
    		System.loadLibrary("console");
    	}
    	
    	/**
    	 * Sets the console color back to it's defaults
    	 */
    	public static void restoreColor()
    	{
    		setColor(0,7);
    	}
    	
    	/**
    	 * Wipes all text off the console screen
    	 */
    	public native static void clearScreen();
    	
    	private native static void setColor(char background, char foreground);
    	
    	/**
    	 * Sets the color of the console in hex the java program is running on may not work on all consoles
    	 * @param background color
    	 * @param foreground color
    	 */
    	public static void setColor(int background, int foreground)
    	{
    		String background2 = Integer.toHexString(background);
    		String foreground2 = Integer.toHexString(foreground);
    		char f=foreground2.charAt(0);
    		char b=background2.charAt(0);
    		setColor(b,f);
    	}
    	
    	/**
    	 * Halts the console IO until the user presses a key on the console 
    	 */
    	public static native void pause();
    	}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JNI Help

    You need to be able to use these tools yourself if you're going to progress. Are you calling javah from the command line? If so, what's wrong with this effort? What's not working right? What error messages are you getting?

  7. #7
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    watch the video mostly invalid class exceptions idk

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JNI Help

    The video doesn't work for me, poor resolution. If you still need our help, consider describing exactly what's going wrong including showing your error messages. If you are running javah on the command line, you can copy the entire text from the cmd window and paste it here.

  9. #9
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    I am running these commands and get this error
    javac Console.java
    javah -jni Console.class


    C:\Users\Joshy\Desktop\console>javac Console.java

    C:\Users\Joshy\Desktop\console>javah -jni Console.class

    Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class
    name: Console.class
    at com.sun.tools.javac.api.JavacTool.getTask(JavacToo l.java:177)
    at com.sun.tools.javac.api.JavacTool.getTask(JavacToo l.java:68)
    at com.sun.tools.javah.JavahTask.run(JavahTask.java:5 09)
    at com.sun.tools.javah.JavahTask.run(JavahTask.java:3 35)
    at com.sun.tools.javah.Main.main(Main.java:46)

  10. #10
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: JNI Help

    Run

    javah - h

    You will then see the line -
    <classes> are specified with their fully qualified names.

    "Fully qualified" means you need the package and class name. e.g. your.package.Console

  11. #11
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    im using the default package
    Last edited by jsobel; 09-23-2012 at 10:14 PM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JNI Help

    I wouldn't do Console.class but would simply use Console and would probably add the classpath option, -cp . For example
    Java Code:
    javah -cp . Console
    Note the period before Console and the spaces around the period, all very important. Again note that I'm using the class's name, Console, but without the .class extension.
    Last edited by Fubarable; 09-23-2012 at 10:31 PM.

  13. #13
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    It worked thanks so much

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JNI Help

    You're welcome. Glad you've got it working. Note that the -h option as was mentioned above gives you "help" information on how to run javah (as noted above).

  15. #15
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: JNI Help

    why do i need to use the -cp option

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JNI Help

    Quote Originally Posted by jsobel View Post
    why do i need to use the -cp option
    -cp is for classpath, and -cp . is telling javah that the classpath should include *this* current directory.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •