Java Class that calls a Perl function : Perl : Development Class : Java examples (example source code) Organized by topic

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JSP
19. JSTL
20. Language Basics
21. Network Protocol
22. PDF RTF
23. Regular Expressions
24. Security
25. Servlets
26. Spring
27. Swing Components
28. Swing JFC
29. SWT JFace Eclipse
30. Threads
31. Tiny Application
32. Velocity
33. XML
Java Tutorial
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Development Class » PerlScreenshots 
Java Class that calls a Perl function

/***************************************************************
 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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.perl.inline.java.*;
// 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[] strsthrows 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[inew 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 s0throws 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;
  }

}



           
       
Related examples in the same category
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.