Calling Perl routines from Java

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Calling Perl routines from Java

This Java example program introduce a Java Class that calls a Perl function.

[edit] Java Source Code

  • Package: example.perl
  • File: StringDistance.java
package example.perl;
 
/***************************************************************
 * Download the jar for org.perl.inline.java first
 ****************************************************************/
 
// Perl file: StringDistance.pl
/*
 * #! /usr/bin/perl # Perl main program acting as a stub for callbacks from Java
 * 
 * use strict; use warnings;
 * 
 * # all modules called from either Perl or from Java must go here: use Text::Levenshtein qw();
 * 
 * use Inline "Java" => "STUDY", # glean interface from Java class file "AUTOSTUDY" => 1, # glean
 * more interfaces, too, just in case "STUDY" => ["StringDistance"], # name of our main Java class
 * "CLASSPATH" => ".", # needed in order to find main Java class ;
 * 
 * my $sd = StringDistance->new(\@ARGV); # construct instance of main Java class $sd->show(); # call
 * routine to show it $sd->StartCallbackLoop(); # prepare to listen for threaded callbacks
 */
 
/**
 * Example of a Java Class that calls a Perl function. <br/> Does not run on its own -- for usage,
 * see StringDistance.pl!
 */
 
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
 
// requires classpath to include this file; usually something like
// .;<perldir>/site/lib/Inline/Java/InlineJavaServer.jar
 
/** The test class. */
public class StringDistance extends InlineJavaPerlCaller {
    JFrame frame; // visual container
    JTextField tf[], dist; // text input fields, result output field
    JButton go, exit; // action buttons
 
    /* The constructor with possibly 2 initial strings */
    public StringDistance(String[] strs) throws InlineJavaException {
	frame = new JFrame("StringDistance");
	Container p = frame.getContentPane();
	p.setLayout(new GridLayout(0, 2));
 
	// The input fields, including labels:
	tf = new JTextField[2];
	for (int i = 0; i < 2; i++) {
	    p.add(new JLabel("String " + i + ":"));
	    tf[i] = new JTextField(20);
	    if ((strs != null) && (i < strs.length))
		tf[i].setText(strs[i]);
	    p.add(tf[i]);
	}
 
	// The output field, including label:
	p.add(new JLabel("Distance:"));
	dist = new JTextField(5);
	dist.setEditable(false);
	p.add(dist);
 
	// The main action button:
	go = new JButton("Compute distance");
	go.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent ae) {
		dist.setText(Integer.toString(match(tf[0].getText(), tf[1].getText())));
	    }
	});
	p.add(go);
 
	// To finish off:
	exit = new JButton("Exit");
	exit.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent ae) {
		frame.dispose();
		System.exit(0);
	    }
	});
	p.add(exit);
 
	if ((strs != null) && (strs.length > 1))
	    dist.setText(Integer.toString(match(tf[0].getText(), tf[1].getText())));
	frame.pack();
    }
 
    // Alternative constructors:
    public StringDistance(String s0, String s1) throws InlineJavaException {
	this(new String[] { s0, s1 });
    }
 
    public StringDistance(String s0) throws InlineJavaException {
	this(new String[] { s0 });
    }
 
    public StringDistance() throws InlineJavaException {
	this((String[]) null);
    }
 
    /** This shows everything */
    public void show() {
	frame.setVisible(true);
    }
 
    /* Optionally for pre-filling the input fields. */
    public void setText(int fieldno, String str) {
	tf[fieldno].setText(str);
    }
 
    /** The central interface function to Perl. */
    public int match(String s0, String s1) {
	try {
	    String str = (String) CallPerl("Text::Levenshtein", "distance", new Object[] { s0, s1 });
	    return Integer.parseInt(str);
	} catch (InlineJavaPerlException e) {
	    System.err.println("Inline Java Perl Exception: " + e);
	} catch (InlineJavaException e) {
	    System.err.println("Inline Java Exception: " + e);
	}
	return 0;
    }
 
}

[edit] What Result You Can Get

Coming soon...


[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools