Mostrando entradas con la etiqueta java. Mostrar todas las entradas
Mostrando entradas con la etiqueta java. Mostrar todas las entradas

Utilizar libreria de C++ desde Java

En visual studio 2008:

1. Crear el archivo java:

public class p
{
   private static native int prueba();
   public static void main(String[] args)
   {
   }
   static {
     System.loadLibrary("Lib2");
     System.out.println(prueba());
   }
}

2. Compilarlo:
          javac p.java

3. Crear el archivo de encabezado (p.h)
         javah -jni p

4. Crear una biblioteca de clases de C++ (llamarla Prueba)
5. Abrir prueba.cpp y agregar el siguiente código:

#include "stdafx.h"#include "p.h"JNIEXPORT jint JNICALL Java_p_prueba
(JNIEnv *jniEnv, jclass clazz)
{
return 12345;}

  6. Compilar la DLL y copiarla a c:\windows\system32

7. Ejecutar
       java p


Redirigir la salida estándar a la impresora en Java


import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; /**  * Main.java  *  * @author www.javadb.com  */ public class Main {          /**      * Redirects System.out and sends all data to a file.      *      */     public void redirectSystemOut() {                  try {                          System.setOut(new PrintStream(new FileOutputStream("system_out.txt")));                      } catch (FileNotFoundException ex) {             ex.printStackTrace();             return;         }                  System.out.println("This won't get displayed on the console, but sent to the file system_out.txt");              }     /**      * Starts the program      *      * @param args the command line arguments      */     public static void main(String[] args) {         new Main().redirectSystemOut();     } }