The Artima Developer Community
Sponsored Link

Java Answers Forum
Matt Gerrans ..Your Code is not working

1 reply on 1 page. Most recent reply: May 19, 2002 5:49 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Janu

Posts: 8
Nickname: janu
Registered: May, 2002

Matt Gerrans ..Your Code is not working Posted: May 19, 2002 4:14 AM
Reply to this message Reply
Advertisement
In DOS (or Windows), cls is an internal command of the command interpreter, so you cannot call it directly from a program. Normally, in a C/C++ program you can solve this problem by calling the command interpreter and having it call the command, e.g.:
system("c:\windows\command.com /c cls");

I tried using exec() on this same string in Java, but it doesn't work, perhaps because Java launches a new hidden process or perhaps for some other reason. Anyway, that leaves two reasonable solutions. The first is to write your own little C/C++ program:

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

and run it with exec() instead. I tried this and it works fine.

The other solution is to make a native method that calls the same clrscr() function. That would consist of writing a java file something like this (exception handling omitted for brevity):
public class ScreenClearer{ private native void ClearScreen(); static { System.loadLibrary("ScreenClearer"); } public static void main(String[] args) { ScreenClearer app = new ScreenClearer(); app.ClearScreen(); }}

Which you would then compile, as usual:

javac ScreenClearer.java

And then use javah to create the C/C++ header file:

javah -jni ScreenClearer

Now, you write your C/C++ file, including that generated header file, which would look like this:

#include <jni.h>
#include "ScreenClearer.h
"#include <conio.h> // Has clrscr().
JNIEXPORT void JNICALL Java_ScreenClearer_ClearScreen(JNIEnv *, jobject)
{
clrscr();
}

And then compile your C/C++ file to a DLL. Here is how you would do that with the (free) Borland 32-bit compiler:

bcc32 -WD -IC:\jdk1.3.1_01\include;C:\jdk1.3.1_01\include\win32 ScreenClearer


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Matt Gerrans ..Your Code is not working Posted: May 19, 2002 5:49 PM
Reply to this message Reply
Yes it is. It is working just fine.

Flat View: This topic has 1 reply on 1 page
Topic: Special information regarding a Java developers? contest Previous Topic   Next Topic Topic: Problems starting Jserv

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use