Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9101473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:08:26+00:00 2026-06-17T01:08:26+00:00

I would like to run class imported from different beanshell file. But I’ve no

  • 0

I would like to run class imported from different beanshell file. But I’ve no idea how instantiate class from main beanshell file. Is this possible?

Class which I import:

class HelloW {
public void run(){
    print("Hello World");
}
}

Main beanshell file which should run and instantiate class:

Interpreter i = new Interpreter();
i.source("HelloW.bsh");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:08:28+00:00Added an answer on June 17, 2026 at 1:08 am

    The BeanShell documentation is pretty good in this area, so you should read through that first. In your case there are few issues. That said, there are Scripted Objects. Also, the .bsh file you start with needs to execute the scripted object. Taking your example this code should work:

    Hello() {
     run(){
         print("Hello World");
     }
    
     return this;
    }
    
    myHello = Hello();
    myHello.run(); // Hello World
    

    *UPDATED answer for version BeanShell 2.0b1 and later which support scripted classes *:

    I created two beanshell files and placed them in a directory “scripts”.

    The first “executor.bsh” is what you are calling the “parent” script, I believe.

    // executor.bsh
    
    addClassPath(".");
    importCommands("scripts");
    
    source(); // This runs the script which defines the class(es)
    
    x = new HelloWorld();
    x.start();
    

    The second file contains the scripted class. Note that I am using a Scripted Command and according to BeanShell documentation, the file name must be the same as the command name.

    // source.bsh
    
    source() {
        public class HelloWorld extends Thread {
            count = 5;
            public void run() {
                for(i=0; i<count; i++)
                    print("Hello World!");
            }
    
        }
    }
    

    I invoked executor.bsh in a java class with:

    Interpreter i = new Interpreter();
    i.source("scripts/executor.bsh");
    
    // Object val = null;
        // val = i.source("scripts/executor.bsh");
    // System.out.println("Class:" + val.getClass().getCanonicalName());
    // Method m = val.getClass().getMethod("start", null);
    // m.invoke(val, null);
    

    Note that I left some commented code which also shows me executing the scripted class from Java, using Reflection. And this is the result:

    Hello World!
    Hello World!
    Hello World!
    Hello World!
    Hello World!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a jar file extracted from my java project to
I would like to run a shell script(.sh) from a Java class. Which is
I would like to run this script (embed drive list in a site) that
I would like to run a script in production. This script generates a record
I would like to run APIDemos under platforms\android-1.5\samples directory on Android emulator but I
I would like to run a Dos program from a web server. The Dos
I would like to run some logic in my Controller class once all IBOutlets
i would like to run a function method of a class inside another function
I found this example on the php site I would like to run a
I would like to run the GWT compiler from my Ant buildfile with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.