⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanshellcommand.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
字号:
/*Copyright, Sam Reid, 2003.*/
package org.jnode.shell.command;

import bsh.Interpreter;
import bsh.EvalError;

import java.io.*;

/**
 * User: Sam Reid
 * Date: Jan 1, 2004
 * Time: 10:40:34 AM
 * Copyright (c) Jan 1, 2004 by Sam Reid
 */
public class BeanshellCommand {
    /**Normal usage will be BeanshellCommand filename.bsh
     * But you could also inline java code like this:
     * BeanshellCommand -code "new JFrame().setVisible(true)";
     *
     * BeanshellCommand should start an interactive session with beanshell.
     * BeanshellCommand file.bsh should interpret and run the script.
     * BeanshellCommand -code "code" should interpret and run the code.
     * */
    public static void showHelp() {
        System.err.println("Usage: \nbsh: interactive beanshell.\n" +
                "bsh -help: this usage menu\n" +
                "bsh -code CODE: evaluate the following shell code.\n" +
                "bsh FILE: run the beanshell interpreter on the source FILE."
        );
    }

    private static void evaluateFile(String arg) {
        try {
            System.err.println("Evaluating beanshell source=" + arg);
            new Interpreter().source(arg);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        } catch (EvalError evalError) {
            evalError.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }
    }

    public static void main(String[] args) {
//        System.out.println("Started beanshell command.");
        if (args.length == 0) {
            //run interactive shell
//            System.err.println("Interactive shell not yet written.");
            runInteractiveShell();
            return;
        } else if (args.length == 1 && args[0].toLowerCase().equals("-help")) {
            System.err.println("Usage: bsh: interactive beanshell. <not yet working>\n" +
                    "bsh FILE: run the beanshell interpreter on the source FILE. <not yet written>\n" +
                    "bsh -help: this usage menu\n" +
                    "bsh -code CODE: evaluate the following shell code."
            );
        } else if (args.length == 1) {
            evaluateFile(args[0]);
        } else if (args.length == 2 && args[0].toLowerCase().equals("-code")) {
            String sourcecode = args[1];
            evaluateSourceString(sourcecode);
        } else {
            showHelp();
        }
    }

    private static void runInteractiveShell() {
        System.err.println("Starting Interactive beanshell.");
        Reader reader = new InputStreamReader(System.in);
        Reader r2 = new BufferedReader(reader);
        PrintStream out = new PrintStream(System.out);
        PrintStream err = new PrintStream(System.err);

        Interpreter interpreter = new Interpreter(r2, out, err, true);
        interpreter.run();
        System.err.println("Finished Interactive beanshell.");
    }

    private static void evaluateSourceString(String sourcecode) {
        System.out.println("Evaluating source string=" + sourcecode);
        Interpreter interpreter = new Interpreter();
//        System.err.println("created interpreter=" + interpreter);

//        i.set("date", new Date());

//        Date date = (Date)i.get("date");    // retrieve a variable
//
//// Eval a statement and get the result
//        i.eval("bar = foo*10");
//        System.out.println( i.get("bar") );
//
//// Source an external script file
//        i.source("somefile.bsh");

// Eval a statement and get the result
//
//        try {
////            interpreter.set("foo", 5);                    // Set variables
////            System.err.println("Set foo = 5");
////            System.err.println("Testing set bar = foo * 10");
////            interpreter.eval("bar = foo*10");
////            System.err.println(interpreter.get("bar"));
//        } catch (EvalError evalError) {
//            evalError.printStackTrace();  //To change body of catch statement use Options | File Templates.
//        }

        try {
            Object out = interpreter.eval(sourcecode);
            System.out.println(out);
        } catch (EvalError evalError) {
            evalError.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -