Java Class that calls a Perl function : Perl « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.