You are here:

Calling 7th functions from Java

Make the Seventh.jar file available to Your program by inserting it into Your jar file on a jar-in-jar basis. For the Eclipse development system You must do the following:
Insert the Seventh.jar file to Your project. Create a new directory named lib (if it does't already exists) in Your projects directory. It should be in the same directory with src an bin now.
To make the Seventh.jar file known to Your project select in the menu  Project – Properties – Java Build Path. On the Tab Libraries use Add External JARs and add the Seventh.jar file.

Then go in the menu to File – Export – Runnable Jar File.
Eclipse's Runnable Jar Export Wizard inserts the Seventh.jar and some more class files beginning with something like org/eclipse/jdt/internal/jarinjarloader/Rsrc… automaticly to Your projects jar file. That makes the 7th stuff accessible from within Your program.
It is now easy to use eg. the num>txt$ word in Your Java program as follows:

  private static final Engine engine = Engine.create();
  String s = (String)engine.eval( "\"12345678\" num>txt$ type cr" );
     …
  engine.shutdown();


The 7th language features are also accessible in the ScriptEngine 7th in the following way:
on Java side:


   public Test() {
      ScriptEngineManager manager = new ScriptEngineManager();
      ScriptEngine engine = manager.getEngineByName( "7th" );
      try {
        engine.getBindings( ScriptContext.GLOBAL_SCOPE ).put( "CALLER", Test.this );
        engine.eval( ": hello javax helloFromJava ; hello" );
      }
      catch( ScriptException ex ) {
        ex.printStackTrace();
      }
   }

   public void helloFromJava() {
     System.err.println( "hello 7th..." );
   }

Now it's possible to communicate in both ways – with javax. You can execute public methods in the calling Java class within 7th.

 

 

A real world example

With the following program You can encrpt an entered text. The encrypted text is stored to the clipboard, so You can paste it with ^V (ctrl V) to a file.

/* * Copyright (c) 2018, 7th Stone Software GmbH. All rights reserved.
 * 7th Stone Software GmbH PROPRIETARY/CONFIDENTIAL.
 * Use is subject to license terms.
 *
 * @author Otmar Klenk
 */

import static java.awt.Font.MONOSPACED;
import static java.awt.Font.PLAIN;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;

import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.border.SoftBevelBorder;
import javax.swing.text.TextAction;
import com.g7th.editor.LastBounds;
import com.g7th.sev.Engine;


public final class Secret extends JApplet {
  private static final long serialVersionUID = 1000L;
  private static final Engine engine = Engine.create();
  private final JTextArea textArea = new JTextArea();
  private final JPasswordField passwdField = new JPasswordField();

  public Secret() {
    textArea.setFont( new Font( MONOSPACED, PLAIN, 18 ) );
    add( textArea );
    textArea.setBorder( new SoftBevelBorder( 2 ) );
    Box box = Box.createHorizontalBox();
    box.add( Box.createHorizontalStrut( 5 ) );
    box.add( new JLabel( "password:" ) );
    box.add( Box.createHorizontalStrut( 5 ) );
    box.add( passwdField );
    passwdField.setMaximumSize( new Dimension( 100, Short.MAX_VALUE ) );
    box.add( Box.createHorizontalStrut( 10 ) );
    box.add( new JButton( new Action7th( "encode" ) ) );
    box.add( new JButton( new Action7th( "decode" ) ) );
    add( box, BorderLayout.SOUTH );
  }

  class Action7th extends TextAction {
    private static final long serialVersionUID = 1000L;

    public Action7th( String name ) {
      super( name );
    }

    @Override
    public void actionPerformed( ActionEvent event ) {
      char[] pwd = passwdField.getPassword();
      if( pwd.length > 0 ) {
        try {
          String s = (String)engine.eval( new StringBuilder().append( "clst \" " )
              .append( textArea.getText().replaceAll( "\n", "\\\\n" ) )
              .append( "\" \"" ).append( pwd ).append( "\" " ).append( getValue( Action.NAME ) )
              .append( "$ drop" ).toString() );
          if( s != null )
            textArea.setText( s );
          if( "encode".equals( event.getActionCommand() ) ) {
            Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
            clip.setContents( new StringSelection( textArea.getText() ), null );
          }
        }
        catch( Throwable ex ) {}  // eg. SecurityException
        finally {
          Arrays.fill( pwd, '\0' );
        }
      }
      else {
        JOptionPane.showMessageDialog( Secret.this, "please enter the password…" );
      }
    }
  }
 
  public static void main( String[] args ) {
    final JFrame frame = new JFrame();
    frame.add( new Secret() );
    frame.setTitle( "— Secret —" );
    frame.setSize( 640, 480 );
    new LastBounds( Secret.class, frame );
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent event ) {
        engine.shutdown();
        frame.dispose();
        System.exit( 0 );
      }
    } );
    frame.setIconImage( new ImageIcon( Secret.class.getResource(
        "com/g7th/sev/res/icon.png" ) ).getImage() );
    frame.setVisible( true );
  }
}