kalo di j2me kan belum ada fungsinya.
nih saya dapat dari http://forums.sun.com/thread.jspa?threadID=341790
public static String URLencode(String s)
{
if (s!=null) {
StringBuffer tmp = new StringBuffer();
int i=0;
try {
while (true) {
int b = (int)s.charAt(i++);
if ((b>=0x30 && b<=0x39) || (b>=0x41 && b<=0x5A) || (b>=0x61 && b<=0x7A)) {
tmp.append((char)b);
}
else {
tmp.append("%");
if (b <= 0xf) tmp.append("0");
tmp.append(Integer.toHexString(b));
}
}
}
catch (Exception e) {}
return tmp.toString();
}
return null;
}
Tuesday, November 03, 2009
Saturday, October 17, 2009
jsp out.print
jika membuat web dengan jsp dan menggunakan out.print atau sebangsanya jangan lupa melakukan out.flush();
kalau tidak nanti akan muncul masalah karena cuma masuk buffer
kalau tidak nanti akan muncul masalah karena cuma masuk buffer
Thursday, October 15, 2009
Friday, October 09, 2009
menjalankan external command di java
import java.io.*;
public class ExternalCommand
{
public static void main(String args[]) throws Exception
{
String line;
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null;
// launch EXE and grab stdin/stdout and stderr
Process process = Runtime.getRuntime ().exec ("bc");
stdin = process.getOutputStream ();
stderr = process.getErrorStream ();
stdout = process.getInputStream ();
// "write" the parms into stdin
line = "13*3" + "\n";
stdin.write(line.getBytes() );
stdin.flush();
stdin.close();
// clean up if any output in stdout
BufferedReader brCleanUp =
new BufferedReader (new InputStreamReader (stdout));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stdout] " + line);
}
brCleanUp.close();
// clean up if any output in stderr
brCleanUp =
new BufferedReader (new InputStreamReader (stderr));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stderr] " + line);
}
brCleanUp.close();
}
}
ref: http://www.rgagnon.com/javadetails/java-0014.html
public class ExternalCommand
{
public static void main(String args[]) throws Exception
{
String line;
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null;
// launch EXE and grab stdin/stdout and stderr
Process process = Runtime.getRuntime ().exec ("bc");
stdin = process.getOutputStream ();
stderr = process.getErrorStream ();
stdout = process.getInputStream ();
// "write" the parms into stdin
line = "13*3" + "\n";
stdin.write(line.getBytes() );
stdin.flush();
stdin.close();
// clean up if any output in stdout
BufferedReader brCleanUp =
new BufferedReader (new InputStreamReader (stdout));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stdout] " + line);
}
brCleanUp.close();
// clean up if any output in stderr
brCleanUp =
new BufferedReader (new InputStreamReader (stderr));
while ((line = brCleanUp.readLine ()) != null) {
System.out.println ("[Stderr] " + line);
}
brCleanUp.close();
}
}
ref: http://www.rgagnon.com/javadetails/java-0014.html
Wednesday, October 07, 2009
mencoba pakai jni
berikut akan saya posting cara untuk membuat helloworld program untuk java native interface
pertama-tama buat file HelloWorld.java
--start--
public class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
--end--
lalu compile dengan
$ javac HelloWorld.java
maka akan menghasilkan HelloWorld.class
buat file header dengan perintah
$ javah HelloWorld
maka akan menghasilkan file HelloWorld.h yang isinya seperti berikut:
(harap file ini jangan diapa2in)
--start--
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
--end--
nah, di file inilah terdapat 'kontrak' yang harus dipenuhi oleh program bahasa c/cpp untuk nantinya mengimplementasikan fungsi yang diperlukan yaitu fungsi print
buat file HelloWorld.c
--start--
#include
#include
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
int i;
for(i=0;i<10;i++)
{
printf("Hello World!\n");
}
return;
}
--end--
compile HelloWorld.c dengan
$ gcc --shared -o libHelloWorld.so -I /usr/lib/jvm/java-6-sun/include/ -I /usr/lib/jvm/java-6-sun/include/linux/ HelloWorld.c /usr/lib/jvm/java-6-sun/jre/lib/i386/server/libjvm.so
(harap sesuaikan dengan sistem anda, saya sendiri menggunakan ubuntu 9.04, sun-java6)
setelah di compile maka akan menghasilkan file libHelloWorld.so
yang nantinya akan di load oleh program java anda, lalu program java akan memanggil fungsi print
coba kita jalankan
$ java -Djava.library.path=. HelloWorld
seharusnya akan memanggil fungsi yang diimplementasikan di HelloWorld.c
dan menghasilkan tulisan Hello World! di layar.
gutlak
pertama-tama buat file HelloWorld.java
--start--
public class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}
--end--
lalu compile dengan
$ javac HelloWorld.java
maka akan menghasilkan HelloWorld.class
buat file header dengan perintah
$ javah HelloWorld
maka akan menghasilkan file HelloWorld.h yang isinya seperti berikut:
(harap file ini jangan diapa2in)
--start--
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
--end--
nah, di file inilah terdapat 'kontrak' yang harus dipenuhi oleh program bahasa c/cpp untuk nantinya mengimplementasikan fungsi yang diperlukan yaitu fungsi print
buat file HelloWorld.c
--start--
#include
#include
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
int i;
for(i=0;i<10;i++)
{
printf("Hello World!\n");
}
return;
}
--end--
compile HelloWorld.c dengan
$ gcc --shared -o libHelloWorld.so -I /usr/lib/jvm/java-6-sun/include/ -I /usr/lib/jvm/java-6-sun/include/linux/ HelloWorld.c /usr/lib/jvm/java-6-sun/jre/lib/i386/server/libjvm.so
(harap sesuaikan dengan sistem anda, saya sendiri menggunakan ubuntu 9.04, sun-java6)
setelah di compile maka akan menghasilkan file libHelloWorld.so
yang nantinya akan di load oleh program java anda, lalu program java akan memanggil fungsi print
coba kita jalankan
$ java -Djava.library.path=. HelloWorld
seharusnya akan memanggil fungsi yang diimplementasikan di HelloWorld.c
dan menghasilkan tulisan Hello World! di layar.
gutlak
Subscribe to:
Posts (Atom)