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 8938195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:34:17+00:00 2026-06-15T10:34:17+00:00

First of all, I’m using Eclipse on MacBook for Java coding. I wrote a

  • 0

First of all, I’m using Eclipse on MacBook for Java coding. I wrote a Python code that uses NLTK “for natural language processing” and it worked nicely. I tried to call a simple Python code from Java and it also worked as expected.

BUT when I tried to call the Python code that uses the NLTK, the import statement fails:
“ImportError: No module named nltk”

It seems that Python was able to locate the NLTK library, but from Java it couldn’t.

I tried to have the import statement in both the Python code and the Java code but with no luck.

Here is the Python code:

#!/usr/local/bin/python
# coding: UTF-8
import os, sys
from nltk.tokenize import word_tokenize # Tokenizer
class RTE:
    def WordPhraseRate(self, Text):
        T_tokens = word_tokenize(Text)
.
.
.

And Here is the Java Code:

import org.python.util.*;
import org.python.core.*;
import org.python.util.PythonInterpreter;

public class NLU_RTE
{
    public PythonInterpreter interpreter = null;
    public NLU_RTE()
    {
        PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
        this.interpreter = new PythonInterpreter();
    }
    void execfile( final String fileName )
    {
        this.interpreter.execfile(fileName);
    }
    PyInstance createClass( final String className, final String opts )
    {
        return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
    }

    public static void main( String gargs[] )
    {
        NLU_RTE ie = new NLU_RTE();
        ie.execfile("/Users/Desktop/nlu.py");
        ie.interpreter.exec("from nltk.tokenize import word_tokenize # Tokenizer");
        String T="About two weeks before the trial started, I was in Shapiro's office in Century City."; // Text
        ie.interpreter.set("T", T);
        PyObject answer = ie.interpreter.eval("RTE('None').WordPhraseRate(T)");
        System.out.print(answer.toString());    

    }
}
  • 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-15T10:34:18+00:00Added an answer on June 15, 2026 at 10:34 am

    I’m not sure what the differences are in performance, but you can just call the script directly in Java if you don’t want to worry about bridging or other problems. Something like the following.

    Python, in a file named testing.py:

    #!/usr/local/bin/python
    # coding: UTF-8
    import os, sys
    from nltk.tokenize import word_tokenize # Tokenizer
    
    if __name__ == "__main__":
        # If you want to read from a file instead of passing data
        #text = open(sys.argv[1]).read()
    
        # read the first argument passed to script
        text = sys.argv[1]
    
        tokens = word_tokenize(text)
        print tokens
    

    Java:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.InputStream;
    import java.io.File;
    
    public class pythonfromjava{
        public static void main(String argv[]) {
            try{
                // for tilda expansion
                //if (filepath.startsWith("~" + File.separator)) {
                    //filepath = System.getProperty("user.home") + filepath.substring(1);
                //}
    
                //ProcessBuilder builder = new ProcessBuilder("python", "-c", "import sys; import nltk; print \"whatever\"");
                ProcessBuilder builder = new ProcessBuilder("python", "testing.py", "four scores and seven years ago");
                builder.redirectErrorStream(true);
                Process p = builder.start();
                InputStream stdout = p.getInputStream();
                BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
    
                String line;
                while ((line = reader.readLine ()) != null) {
                    System.out.println ("Stdout: " + line);
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I'm quite new to the Android and JAVA world (coming from
First of all I want to mention two things, One: My code isn't perfect
First of all, let me say that I am not a professional coder -
First of all, I know how to build a Java application. But I have
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
First all, I am still learning CakePHP and I am close to ZERO using
First of all, let me clarify that I know you cannot have actual abstract
First of all, I got this huge xml file that represents data collected by
First of all I agree that mocking external API calls is the right thing
i have this code which i use in order to extract first all users

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.