最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can Java and JavaScript work together? - Stack Overflow

programmeradmin1浏览0评论

I'll preface this by stating that I know Java is not JavaScript and vice versa.

I've got a project where I need to count occurrences of words for each of 1750 document names and document contents. I've got some awesome JavaScript from a colleague that does exactly what I want from a form input on a web page.

I want to use Java's FileReader, BufferedReader, walkFileTree, etc. to traverse the directories in which the documents live.

I'm not sure if this is the most efficient or effective approach, but both the Java and JavaScript parts of the code are working independently of one another now, and I'd like to see if I can get them to pass data between them before I start re-inventing the wheel.

Here's where I am so far. I'm stuck at the CLParse method & have inserted pseudocode:

public static void main(String... aArgs) throws FileNotFoundException    {
    File startingDirectory= new File("CGT");
    List<File> files = FileListing.getFileListingNoSort(startingDirectory);
    for(File file : files )           {
        CLParse(file.toString());
    }   }

static private List<File> getFileListingNoSort(File aDirectory) throws FileNotFoundException    {
    List<File> result = new ArrayList<File>();
    File[] filesAndDirs = aDirectory.listFiles();
    List<File> filesDirs = Arrays.asList(filesAndDirs);
    for(File file : filesDirs)          {
        result.add(file); //always add, even if directory
        if ( ! file.isFile() )   {
            List<File> deeperList = getFileListingNoSort(file);
            result.addAll(deeperList);
        }      }      
    return result;
    }

 /* is something like this doable and how would I do it?
 */
public static void CLParse(String fn) {
      pass fn to JavaScript counter
      return an array of {word,occurences} for the string
      write array to file
      }

I'll be creating another set of methods to extract and pass the document CONTENTS as a string as well. I'd love to know if anyone has any practical experience passing values back and forth between Java and JavaScript, and advice on a good/better way to do it.

I'll preface this by stating that I know Java is not JavaScript and vice versa.

I've got a project where I need to count occurrences of words for each of 1750 document names and document contents. I've got some awesome JavaScript from a colleague that does exactly what I want from a form input on a web page.

I want to use Java's FileReader, BufferedReader, walkFileTree, etc. to traverse the directories in which the documents live.

I'm not sure if this is the most efficient or effective approach, but both the Java and JavaScript parts of the code are working independently of one another now, and I'd like to see if I can get them to pass data between them before I start re-inventing the wheel.

Here's where I am so far. I'm stuck at the CLParse method & have inserted pseudocode:

public static void main(String... aArgs) throws FileNotFoundException    {
    File startingDirectory= new File("CGT");
    List<File> files = FileListing.getFileListingNoSort(startingDirectory);
    for(File file : files )           {
        CLParse(file.toString());
    }   }

static private List<File> getFileListingNoSort(File aDirectory) throws FileNotFoundException    {
    List<File> result = new ArrayList<File>();
    File[] filesAndDirs = aDirectory.listFiles();
    List<File> filesDirs = Arrays.asList(filesAndDirs);
    for(File file : filesDirs)          {
        result.add(file); //always add, even if directory
        if ( ! file.isFile() )   {
            List<File> deeperList = getFileListingNoSort(file);
            result.addAll(deeperList);
        }      }      
    return result;
    }

 /* is something like this doable and how would I do it?
 */
public static void CLParse(String fn) {
      pass fn to JavaScript counter
      return an array of {word,occurences} for the string
      write array to file
      }

I'll be creating another set of methods to extract and pass the document CONTENTS as a string as well. I'd love to know if anyone has any practical experience passing values back and forth between Java and JavaScript, and advice on a good/better way to do it.

Share Improve this question edited Dec 10, 2015 at 10:06 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 12, 2012 at 12:45 dwwilson66dwwilson66 7,07428 gold badges76 silver badges123 bronze badges 6
  • You can call JavaScript from Java using the Java Scripting Framework, but in this case, it's probably easiest to just translate your colleague's code to Java. It's not really long, is it? – Thilo Commented Dec 12, 2012 at 12:48
  • 2 Java has a 'scripting engine' for evaluating for example JavaScript from Java. See docs.oracle./javase/6/docs/technotes/guides/scripting/…. Is this what you need? – Mathias Schwarz Commented Dec 12, 2012 at 12:48
  • If your JavaScript code is based on the DOM structure of the document and you really, really want to reuse it, then you can write a browser plugin (for example for Chrome), let the Java code start browser instances on the right pages, and let the plugin send data back to the Java code through AJAX. I would rewrite the JavaScript code... – Mathias Schwarz Commented Dec 12, 2012 at 12:51
  • The existing code is about 250 lines. I've never really reverse engineered JavaScript into something else; I'm nervous about messing something up in the translation. I'll look into the Java Scripting framework and see which looks more intimidating. :) – dwwilson66 Commented Dec 12, 2012 at 12:52
  • Do you need the results in your Java code? Because a better alternative might be to run the JavaScript directly on the mand line. – Perception Commented Dec 12, 2012 at 12:52
 |  Show 1 more ment

4 Answers 4

Reset to default 1

You got 2 Options to let them interact with each other, which i know:

1.Applet <-> javascript

2.Serlvet <-> javascript

With option 1, you have to build a Communication with a JSObject: JSObject

or you cann call the Applets Method instanstly with document.appletname.methodname(); with this you can even Parse same simply Formats to each other.

With Option 2 you have to build a munication with a Servlet.

in here you have to send an Ajax request to the the Servlet:

$.post('login',{name:"Peter", pw:"123456"},function() 
{ 
   //do whatever
})

JavaServlet class

the first ment, has to written as an Servlet in your web.xml, it´s the servlet pattern. the second ones, are the parameters which can be read in the servlet. the function describes the stuff, which can be done in the request.

The differences between these two Options are:

1.the Applets runs on the users Computer, so you can access his files. But for this your applet has to be signed.

2.the Servlet runs on the Server. Here you have got full file access(if the system allows you too have it).

I would try to investigate Mozilla Rhino.
http://en.wikipedia/wiki/Rhino_%28JavaScript_engine%29

Check out Rhino https://developer.mozilla/en-US/docs/Rhino

You can create java objects and use them in javascript. Integration is straightforward

You can use AJAX to send and receive values to server. You can send parameters or JSON to server and get response.

You can use JSONP to serve the data, or if you have no control of the second server, use a reverse proxy to proxy requests to the second server through the first.

发布评论

评论列表(0)

  1. 暂无评论