Where to Install the Import Modules Used in a Java Program

Where to install the import modules used in a java program?

Most of those classes are available in JSE, but it looks like it's also using JMF. You can download JMF from here:
JMF Download

You don't install modules, you just add jars/classes to the classpath. How you do that depends on if you are using an IDE or not. This article can give you some insight on how things work:
PATH and CLASSPATH

how to add module of python into java using jython jar

The following code runs just fine on my Ubuntu box with Jython 2.7 and Java 1.6 (tested with Eclipse and from the terminal):

package myjythonproject;
import org.python.util.PythonInterpreter;

public class MyJythonProject {

public static void main(String[] args) {
try
{
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("/home/vicent/foo.py");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Just make sure to compile and execute with jython.jar in your classpath.

UPDATE

I've just installed NetBeans 7.2.1 (version for Java SE) on my Ubuntu box, created a new Java project, MyJythonProject, and added the code shown above to the MyJythonProject.java file. Then on the Properties dialog of the project I've selected Libraries in the left pane. In the right pane I've selected the Compile tab, clicked the Add JARF/folder button and selected my jython jar (/opt/jython2.7a2/jython.jar). The I've closed the Dialog and in the Run menu of the main window I've selected Clean and Build Project (MyJythonProject). After that I've run the project and it works like a charm. No Python/Jython plugin required, just tell to your project where the jython.jar is installed.

UPDATE 2

Also notice that Python3 is not supported by Jython so you have to use a Python2.x interpreter.

Jython and python modules

You embed jython and you will use some Python-Modules somewere:

if you want to set the path (sys.path) in your Java-Code :

public void init() {
interp = new PythonInterpreter(null, new PySystemState());

PySystemState sys = Py.getSystemState();
sys.path.append(new PyString(rootPath));
sys.path.append(new PyString(modulesDir));
}

Py is in org.python.core.

rootPath and modulesDir is where YOU want !

let rootPath point where you located the standard-jython-lib

Have a look at src/org/python/util/PyServlet.java in the Jython-Source-Code for example

Adding in a python module to the sys.path for a Java project in pydev, Eclipse

Having spent a few seconds looking at the documentation (so I've no ide if this actually works), it looks like you can use pass in a PySystemState to the PythonInterpreter constructor. The PySystemState has a public path field that (I assume) you can append path entries to.

UPDATE

As you want to specify the path in the runtime configuration, then it looks like you can do this by specifying the python.path system property on the command line (from the Jython FAQ):

Properties props = new Properties();
// set in the VM args in the Eclipse runtime configuration instead
// props.setProperty("python.path", "/home/modules:scripts");
PythonInterpreter.initialize(System.getProperties(), props,
new String[] {""});

How can I install various Python libraries in Jython?

Some Python modules, like lxml, have required components in C. These won't work in Jython.

Most Python packages will work fine, and you can install them using the same tools as you use in CPython. This is described in Appendix A of Jython Book:

To get setuptools, download ez_setup.py from
http://peak.telecommunity.com/dist/ez_setup.py. Then, go to the
directory where you left the downloaded file and execute:

$ jython ez_setup.py

[The easy_install script will be] installed to the bin directory of
the Jython installation (/home/lsoto/jython2.5.0/bin in the example
above). If you work frequently with Jython, it’s a good idea to
prepend this directory to the PATH environment variable, so you don’t
have to type the whole path each time you want to use easy_install or
other scripts installed to this directory.

Testing it myself, after installing setuptools in Jython, pip installed correctly:

$ sudo /usr/bin/jython2.5.2b1/bin/easy_install pip
Searching for pip
[...]
Installing pip-2.5 script to /usr/bin/jython2.5.2b1/bin
Installing pip script to /usr/bin/jython2.5.2b1/bin

Installed /usr/bin/jython2.5.2b1/Lib/site-packages/pip-1.0.2-py2.5.egg
Processing dependencies for pip
Finished processing dependencies for pip

$ sudo /usr/bin/jython2.5.2b1/bin/pip install bottle
Downloading/unpacking bottle
Downloading bottle-0.9.6.tar.gz (45Kb): 45Kb downloaded
Running setup.py egg_info for package bottle
Installing collected packages: bottle
Running setup.py install for bottle
Successfully installed bottle
Cleaning up...

$ jython
Jython 2.5.2b1 (Release_2_5_2beta1:7075, Jun 28 2010, 07:44:20)
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_26
Type "help", "copyright", "credits" or "license" for more information.
>>> import bottle
>>> bottle
<module 'bottle' from '/usr/bin/jython2.5.2b1/Lib/site-packages/bottle$py.class'>
>>>

Can we call another project java class from our project in eclipse

You can do either this way:

In the dependency you can add the other projects to your project. Right click on project -> properties -> java build path -> projects. Add your project here.

OR

Make the classes of project into jar and add it to other project

Dependencies should be added in classpath

In run time, make sure the JAR files of the referenced projects is
added in class path on both the cases.



Related Topics



Leave a reply



Submit