[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 831
  • Last Modified:

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
0
bin571
Asked:
bin571
  • 5
  • 4
  • 3
  • +1
3 Solutions
 
objectsCommented:
ClearScreen clr = new ClearScreen();
clr.clearScreen();
0
 
bin571Author Commented:
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?

0
 
objectsCommented:
> 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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bin571Author Commented:
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++ :(
0
 
ashok3sepCommented:
the functionality name in c is clrscr();

you can use as

clrscr();
     or
system("cls");


it is present in the console Input/Output

so you should link your conio.h in vc++ in the linker libraries of the dll.


and then compile the c class file .

http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/

0
 
bin571Author Commented:
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 :(
0
 
ashok3sepCommented:
since you have uninstalled java you have to again link the JNI.h and an another file jni_md.h to the c file
0
 
ashok3sepCommented:
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.
0
 
objectsCommented:
its not having a problem with compiling, its the linking that is having the problem.
0
 
bin571Author Commented:
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!!!
0
 
ashok3sepCommented:
have you set  the PATH variable correctly

you can do that and check for it.

set PATH = c:\compiler Directory\bin;
0
 
ashok3sepCommented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> But now, I can't even compile the file like -
>> void main (void){
>>      clrscr();
>>      return 0;
>>}

That's because you have defined main () as 'void' but you are still trying to return a value from it (0). Either change:

>> return 0 ;

to:

return ;

Or change:

>> void main ()

to:

int main ()

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now