Reading from file then excetuing Js code by calling specific method

I made some .txt files, which contains only one method of JavaScript. For example the content of code.txt is:

function method(){

    return 4;

}

Now I want to read specific file and execute java script code

This is what I have tried:

public class read {


    public static void main(String[] args) throws IOException, ScriptException {


        ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("js");

        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("code.txt")));

        String content ="";
        String code ="";

        while((content = bufferedReader.readLine())!=null){
            code+=content;
        }
        
        scriptEngine.eval(code);
    }
}

I want to get the result from the method() and check if returned value is 4

139 thoughts on “Reading from file then excetuing Js code by calling specific method”

  1. Pretty section of content. I just stumbled upon your
    weblog and in accession capital to assert that I get in fact enjoyed account your blog posts.
    Anyway I will be subscribing to your augment and even I achievement you access consistently
    fast.

    Reply
  2. Cool blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple tweeks would really make my
    blog shine. Please let me know where you got your theme.
    Kudos

    Reply
  3. Hey there just wanted to give you a quick heads up.
    The words in your article seem to be running
    off the screen in Safari. I’m not sure if this is a
    format issue or something to do with web browser compatibility but I
    thought I’d post to let you know. The layout look great though!

    Hope you get the issue resolved soon. Many thanks

    Reply
  4. Greetings from Colorado! I’m bored to death at work so
    I decided to browse your site on my iphone during lunch break.

    I really like the knowledge you present here and can’t wait
    to take a look when I get home. I’m amazed at how quick your
    blog loaded on my cell phone .. I’m not even using
    WIFI, just 3G .. Anyways, good blog!

    Reply
  5. Hey, I think your blog might be having browser compatibility issues.

    When I look at your blog in Ie, it looks fine
    but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, wonderful blog!

    Reply
  6. Hello there! This article couldn’t be written much better!
    Looking through this article reminds me of my previous roommate!
    He continually kept talking about this. I will forward this article to him.
    Pretty sure he’ll have a great read. Thanks for sharing!

    Reply
  7. I was curious if you ever thought of changing the page layout of your blog?
    Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having 1 or
    two images. Maybe you could space it out better?

    Reply
  8. Amazing blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would
    really make my blog shine. Please let me know where you got your theme.
    With thanks

    Reply
  9. A motivating discussion is worth comment. I do think that you
    need to publish more on this subject matter, it may not be a taboo matter but typically people don’t discuss such topics.

    To the next! Cheers!!

    Reply
  10. Wonderful work! That is the type of information that are supposed to be shared around the web.
    Disgrace on the search engines for no longer positioning this post upper!
    Come on over and seek advice from my web site . Thank you =)

    Reply
  11. We are a group of volunteers and opening a new scheme in our community.
    Your web site provided us with valuable info to work on. You’ve done an impressive job and our entire community will be grateful to you.

    Reply
  12. Hi! Someone in my Myspace group shared this site with us so I came to check it
    out. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers!
    Great blog and amazing design.

    Reply
  13. I’m truly enjoying the design and layout of your website. It’s a very easy
    on the eyes which makes it much more enjoyable for me to come here and visit
    more often. Did you hire out a developer
    to create your theme? Outstanding work!

    Reply
  14. After looking into a number of the articles
    on your web site, I honestly like your way of writing a
    blog. I bookmarked it to my bookmark site list and will
    be checking back in the near future. Please visit my website as well and let me know what you think.

    Feel free to visit my page raycon

    Reply
  15. Great post. I was checking continuously this weblog and I’m inspired!
    Very helpful information specially the final section 🙂 I handle such info a lot.

    I used to be seeking this certain info for a very long time.
    Thanks and good luck.

    Reply
  16. Undeniably believe that which you stated. Your favorite justification seemed
    to be on the web the simplest thing to be aware of.
    I say to you, I definitely get annoyed while people think about worries that they just do not know about.
    You managed to hit the nail upon the top as well as defined
    out the whole thing without having side-effects , people can take
    a signal. Will likely be back to get more. Thanks

    Reply
  17. Hi there, just became alert to your blog through Google, and found that it is truly informative.
    I’m gonna watch out for brussels. I’ll appreciate if you continue this in future.
    Many people will be benefited from your writing. Cheers!

    Reply
  18. Hiya very nice blog!! Man .. Beautiful ..
    Amazing .. I’ll bookmark your blog and take the feeds
    also? I’m happy to find a lot of useful information here within the post, we want develop
    extra strategies on this regard, thanks for sharing. .
    . . . .

    Here is my webpage – tracfone special

    Reply

Leave a Comment