Link to home
Start Free TrialLog in
Avatar of bin571
bin571

asked on

How to call the method clearScreen()?

System Information -
1. WindowXP
2. Visual Studio 6 - MS C++ 6.0
3. Java2 Platform, Standard Edition 1.4.2_08 SDK

Please follow my steps and teach me how to fix the problem -
1. Create a Java class named ClearScreen that declares a native method.

class ClearScreen {
    public native void clearScreen();

    static {
        System.loadLibrary("clearscreen");
    }
   
    public static void main(String[] args) {
       new ClearScreen().clearScreen();
    }
}

2. Use javac to compile the Java code

3. Use javah to create a JNI-style header file (a .h file) from the ClearScreen class

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ClearScreen */

#ifndef _Included_ClearScreen
#define _Included_ClearScreen
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     ClearScreen
 * Method:    clearScreen
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_ClearScreen_clearScreen
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif



4. Write the implementation for the native method in a native language (such as ANSI C) source file and saved as ClearScreenImp.c

#include <jni.h>
#include "ClearScreen.h"
#include <stdio.h>
#include <conio.h>

JNIEXPORT void JNICALL
Java_ClearScreen_clearScreen(JNIEnv *env, jobject obj)
{
    cls();
    return o;
}

Now, I have the clearscreen.dll, clearscreen.lib, ClearScreenImp.obj, ClearScreen.class ...
My question is how can I call this clearScreen() within my java application program?
e.g.

public class Test{

        public static void main(String [] args) throws Exception{
                 
                    System.out.println("Hello World!!!");
                    // now I want to clear my console, and continue to do something....
                    // how to invoke the clearScreen() ?
       }
}

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bin571
bin571

ASKER

hmmmm.......
#include <jni.h>
#include "ClearScreen.h"
#include <stdio.h>
#include <conio.h>

JNIEXPORT void JNICALL
Java_ClearScreen_clearScreen(JNIEnv *env, jobject obj)
{
    printf("Hello world");
    return o;
}

This one works....so I change the printf("Hello world") to cls(), then it doesn't work :(

#include <jni.h>
#include "ClearScreen.h"
#include <stdio.h>
#include <conio.h>

JNIEXPORT void JNICALL
Java_ClearScreen_clearScreen(JNIEnv *env, jobject obj)
{
    cls();
    return o;
}

ClearScreenImp.obj: error LNK2001: unresolved external symbol _cls
clearscreen.dll : fatal error LNK1120: 1 unresolved externals.

But, I have compiled the below file successfully before, now I can't compile anymore (got the same error message as above)

#include <stdio.h>
#include <conio.h>
void main (void){
      clrscr();
      return 0;
}

Any idea?

> ClearScreenImp.obj: error LNK2001: unresolved external symbol _cls

Thats saying you haven't told the linker where to find cls.

> now I can't compile anymore (got the same error message as above)

Have you changed the command you use to compile/link

Avatar of bin571

ASKER

I didn't change anything, but I uninstalled the j2ee and installed the j2se. Then, set the path (j2se).
I haven't changed anything about the visual C++ :(
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bin571

ASKER

I have gone through all the steps. But now, I can't even compile the file like -

#include <stdio.h>
#include <conio.h>
void main (void){
      clrscr();
      return 0;
}

e.g cl try.c

but I don't have any problem to compile this file before :(
since you have uninstalled java you have to again link the JNI.h and an another file jni_md.h to the c file
also say the c file where to look for the conio.h and stdio.h header files.


then compile it and should work for you.
its not having a problem with compiling, its the linking that is having the problem.
Avatar of bin571

ASKER

the thing is I rewrited a simple c file named abc.c

#include <stdio.h>
#include <conio.h>
void main (void){
      clrscr();
      return 0;
}

and compiled this file c:\ cl abc.c
I can't even compile abc.c!!!
have you set  the PATH variable correctly

you can do that and check for it.

set PATH = c:\compiler Directory\bin;
Follow the link below to create a dll of a c program that lijks to your Java program and hope this helps you

http://www.functionx.com/visualc/libraries/win32dll.htm
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial