call python script from Java app

Hello readers,

in this post I wanna give you a brief description how you can call python code from with a java program. For that purpose we will use two very simple “Hello World” programs, one in python, one for java.

First the python script. Create a new file “~/python/helloPython.py” with the following content:

import os
from stat import *

print "Hello World from python"

Now fire up your eclipse and create a default java application. Create a new java class named “PythonCaller”. The content of this new file will look like this:

package org.norbert;

import java.io.*;

public class PythonCaller {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// set up the command and parameter
String pythonScriptPath = "/home/norbert/python/helloPython.py";
String[] cmd = new String[2];
cmd[0] = "python"; // check version of installed python: python -V
cmd[1] = pythonScriptPath;

// create runtime to execute external command
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);

// retrieve output from python script
BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while((line = bfr.readLine()) != null) {
// display each output line form python script
System.out.println(line);
}
}
}

So what is this doing? Basically we execute the python script by envoking python2.6 and provide the path to the script as parameter, as you would normally do it if you exec the script in a terminal/bash.
The output generated by the script is then read line by line with a BufferedReader and displayed in the terminal where we execute the java application.

You can execute the java program in eclipse by click on the green run button or compile and exec it in a terminal with:

cd workspace/JavaPythonCall/src/org/norbert
javac PythonCaller.java
cd ../..
java org.norbert.PythonCaller

result:

Hello World from python

Sweet ^_^ yeah 😉 This was a quick one, but I hope this helps.
cheers
norbert

Android AVD Manager

Hello there,
in this post I briefly explain how you can create new virtual device for your Android emulator on your computer.
If you wanna develop applications for Android and don’t have an Android mobile phone at hand, you can start with the emulator coming along with the Android SDK. By the way, you get the Android SDK for free here: https://developer.android.com/sdk/index.html

Continue reading

Master Thesis – Near Field Communication

Hello everybody,

I know its quite a while since I posted anything here on my webblog, but now I am working on a new project that is really exciting. I am dealing with NFC (Near Field Communication) in the context of my Master Thesis here at TU Munich. The last months I did research on this topic and found that this technique is quite old BUT this year (2012) a great breakthrough is predicted.

In the upcoming blogpost I am going to keep you up to date of the progress of my thesis and the interesting stuff I stumble upon 🙂 So if I draw your interesst please check back on my blog for more interessting stuff 😉

cheers 😉

Textverarbeitung vs. Textsatz – auf welcher Seite der Macht stehst Du?

Hallo und guten Abend liebe LeserInnen,

LaTeX vs. MS Word – oder – Textsatz vs. Textverarbeitung, das ist hier die Frage 😉 die sich diese Woche stellt. Will man eine Seminar/Semester oder Abschlussarbeit schreiben steht man schnell vor der Wahl, was eingesetzt werden soll. Nicht außer acht sollte man auch Tools zur Literaturverwaltung lassen und wie deren Anbindung an das gewählte System zum Schreiben der Arbeit beschaffen ist.

Diese Woche sollen wir im Kurs Informationskompetenz darüber reflektieren – wobei jeder der meine bisherigen Artikel in dieser Rubrik gelesen hat bereits herausgehört haben dürfte, für welches Lager meine Wahl ausfallen wird 😉

Continue reading

Was ist eigentlich ein Petabyte ?

Hallo liebe Leser meines Blogs,

der Wetterfrosch im Radio heute morgen beim Aufstehen hatte sich um ne gute Stunde verschätzt: Es hieß gegen 17:00 soll es hier in München regnen, tja aber seit ca. ner halben Stunde regnet es bereits und nun ist gerade kurz nach vier. Naja sei’s drum, vor lauter Klausurvorbereitung für morgen konnte ich ebensowenig vom sonnigen Wetter heute Vormittag profitieren.

In diesem Eintrag möchte ich, wie jede Woche, zu meinem Kurs Informationskompetenz die Leitfragen beantworten und über die Videotutorials sowie die zusätzlichen Unterlagen reflektieren, aber eins nach dem anderen. Continue reading

Ovid – was sonst

Hallo geehrter Leser meines Blogs,

kommenden Montag steht bei mir bereits die erste Klausur in diesem Semester an. Ich schreibe Informatikrecht im Bereich Überfachliche Grundlagen bei Herrn Bernd Harder. Durch die Klausurvorbereitung während des Wochenendes ging mein Blogeintrag etwas unter und kommt daher etwas später als sonst üblich. Continue reading

Shibboleth – oder die Relevanz der thematischen Recherche O_o

Guten Abend liebe Leser meines Blogs,

es ist wieder an der Zeit meinen Eintrag für meinen Kurs Informationskompetenz zu verfassen. Diese Woche war das Thema gezielte thematische Suche nach Aufsätzen (oder Papern) für eine Semester/Seminar/Abschlussarbeit. Ein Hauptaspekt lag dabei auf dem ISI Web of Knowledge und speziell auf dem Web of Science. Continue reading

Zeitschriften und SFX – nein nicht Science Fiction ;)

Servus und Hallo liebe Leser,

mein heutiger Blogeintrag befasst sich mit den Leitfragen zu meinem Kurs “Informationskompetenz” in der KW 25/26. Ich möchte auf meine Verwendung von Zeitschriftenartikeln, mein Einsatz von Datenbanken und Suchmaschinen sowie auf die SFX (Service, Fernleihe und weitere eXtras) eingehen.

Continue reading

Fachzeitschriften und Prüfungsfragen

Hallo liebe Leser meines Blogs,

dieses mal hat sich die Hausaufgabe in zwei Teile gegliedert:

  • Konzeptionierung einer Prüfungsaufgabe zum Themengebiet 3 – Literaturrecherche
  • Blogeintrag zum Thema Fachzeitschriften

Aus diesem Grund (und wegen den Pfingstfeiertagen 😉 ) war die Bearbeitungszeit auch auf 2 Wochen angesetzt. Heute möchte ich meinen Blogeintrag verfassen und mich mit meinem Umgang von Fachzeitschriften auseinandersetzen. Continue reading