How can we use external Jython modules with JSR-223? (Assuming some foo python module is installed using pip or easy_install and the following Python code is running with Jython)
Python code: (jsr223_test.py)
import pyfoo
pyfoo.do_sth()
Java code:
import javax.script.*;
import java.io.*;
import org.python.core.Py;
import org.python.core.PySystemState;
public class Main() {
//BEGIN EDIT
PySystemState engineSys = new PySystemState();
engineSys.path.append(".");
Py.setSystemState(engineSys);
//END EDIT
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jython = mgr.getEngineByName("jython");
jython.eval(new FileReader(new File("jsr223_test.py")));
}
This results in ImportError: no module named pyfoo. However executing the following in a terminal succeeds.
jython jsr223_test.py
Any advice?
Edit:
I guessed it was a classpath issue and copied the py files from the eggs to my applications working directory. Following the http://wiki.python.org/jython/UserGuide#using-jsr-223 I've also added that path to the engine environment (as seen on the code, between //BEGIN EDIT and //END EDIT) and it worked.
I've also tried adding $JYTHON_HOME/Lib/site-packages to the path but it did not work, I don't know why.
Related
My distro doesn't provide a 'selenium' package
$ apt search selenium p libtest-www-selenium-perl - Perl test framework using Selenium Remote Control i A python3-selenium - Python3 bindings for Selenium p qunit-selenium - Run QUnit tests through Selenium WebDriver p ruby-selenium-webdriver.
I've tried this and this approaches and the browser does get invoked successfully. This is my code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import selenium.webdriver;
//import selenium.webdriver.support.ui.WebDriverWait;
//import selenium.webdriver.common.by.By;
//import selenium.webdriver.support.expected_conditions;
import static org.openqa.selenium.By.*;
//import static sun.security.util.KnownOIDs.EC;
public class Login {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "./geckodriver");
WebDriver dr = new FirefoxDriver();
dr.get("https://google.com/");
dr.manage().window().maximize();
//Click on "Join now"
WebElement join = dr.findElement(xpath("//a[#class='newUser green']"));
//WebDriverWait(driver, 20).until(EC.element_to_be_clickable((xpath("//a[#class='newUser green']")))).click();
join.click();
}
}
My problem is uncommenting any of the imports breaks because java has no idea what a selenium is. How do I add it? Linux Mint.
Go to https://chromedriver.chromium.org/downloads page
Depends on your Chrome version, Download Linux chrome driver zip package
Unip package and locate under any folder that is in system PATH or add your folder to PATH
Go to https://www.selenium.dev/downloads/ and download Selenium Java package and locate it under your project's library package(depends on your project/IDE settings)
Welcome. There is a lot going on in your quesrion: intelij; Java class paths; import statements.
I recommend tackling it one at a time.
copy the java example on selenium.dev doco page.
Run commandline javac.exe directly javac command examples (code Java.net) look for the 'Compile a source file which depends on multiple libraries' example. NOTE: the import statements are class references... Don't use the filename (is different to python).
Search Web for how to configure classpath for intelij.
I am trying to work through what should be a simple problem, but am missing something. I have a very simple Java class:
package blah.blah;
public class Tester {
public String testMethod1() { return "GotHere"; }
public String testMethod2() { return "GotHereToo"; }
} // Tester
I am trying to get it to load into JPype:
import jpype
import jpype.imports
from jpype.types import *
path_to_jvm = "C:\\Java\\OracleJDK-8_241_x64\\jre\\bin\\server\\jvm.dll"
jpype.startJVM(path_to_jvm, classpath=["C:\\Users\\Administrator\\eclipse-workspace\\JpypeTest\\src;"])
from java.lang import System
print(System.getProperty("java.class.path"))
from java.io import ObjectInputStream
from blah.blah import Tester
tester = Tester()
print(tester.testMethod1())
print(tester.testMethod2())
jpype.shutdownJVM()
Everything works fine when I load the class directly using the above code. It stops working when I try to get it to load the Class file via a JAR.
jpype.startJVM(path_to_jvm, classpath=["C:\\Users\\Administrator\\JpypeTest.jar;"])
The error I get is:
Traceback (most recent call last):
File ".\jpype_test.py", line 16, in <module>
from blah.blah import Tester
ModuleNotFoundError: No module named 'blah'
PS C:\Users\Administrator> python .\jpype_test.py
C:\Users\Administrator\JpypeTest.jar;
Traceback (most recent call last):
File ".\jpype_test.py", line 16, in <module>
from blah.blah import Tester
ModuleNotFoundError: No module named 'blah'
I have built the JAR file a few different ways from Eclipse 2020/09 using JAR export and from the command line using:
C:\Users\Administrator\eclipse-workspace\JpypeTest\src>"C:\Java\OracleJDK-8_241_x64\bin"\jar cf C:\Users\Administrator\JpypeTest.jar blah\blah\*.class
I have confirmed that the JVM and the python interpreter are both 64 bit. I have also done my best to ensure the JVM is exactly the same between Eclipse, the command line and Python.
From what I can see, the JAR looks fine regardless of how I build it. It contains the blah\blah directory and the Tester.class file under it. The manifest is the only other file in the JAR.
I have also tried creating the JAR file with the class file at different directory levels (ie. one level of blah directories and no levels of blah directory).
Here are the versions of the python software:
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Package Version
------------ -------------------
certifi 2020.6.20
JPype1 1.0.2
pip 20.2.3
setuptools 50.3.0.post20201006
wheel 0.35.1
wincertstore 0.2
I clearly have the base plumbing working since it can see the class file. Any thoughts on why the JAR is failing?
Thanks a bunch for your consideration.
Check that you don't have a directory called "blah" that python would look in for modules. This would hide your Java classes, as per https://jpype.readthedocs.io/en/latest/userguide.html#importing-java-classes :
One important caveat when dealing with importing Java modules. Python always imports local directories as modules before calling the Java importer. So any directory named java, com, or org will hide corresponding Java package. We recommend against naming directories as java or top level domain.
I'm getting an InvalidJarIndexException when trying to utilize Jython standalone JAR inside my application and I'm unable to figure out what I'm doing wrong.
As soon as I attempt to execute a Python script with an import statement for any Java class from a package starting with "com.", e.g.: "com.foo.Bar", the following exception is thrown (truncated):
Traceback (most recent call last):
File "<string>", line 1, in <module>
sun.misc.InvalidJarIndexException: Invalid index
at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:1152)
at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:1062)
at sun.misc.URLClassPath$JarLoader.findResource(URLClassPath.java:1032)
at sun.misc.URLClassPath.findResource(URLClassPath.java:225)
at java.net.URLClassLoader$2.run(URLClassLoader.java:572)
at java.net.URLClassLoader$2.run(URLClassLoader.java:570)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(URLClassLoader.java:569)
at java.lang.ClassLoader.getResource(ClassLoader.java:1089)
at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:233)
at org.python.core.ClasspathPyImporter.tryClassLoader(ClasspathPyImporter.java:221)
at org.python.core.ClasspathPyImporter.makeEntry(ClasspathPyImporter.java:208)
at org.python.core.ClasspathPyImporter.makeEntry(ClasspathPyImporter.java:18)
at org.python.core.util.importer.getModuleInfo(importer.java:174)
at org.python.core.util.importer.importer_find_module(importer.java:98)
at org.python.core.ClasspathPyImporter.ClasspathPyImporter_find_module(ClasspathPyImporter.java:134)
at org.python.core.ClasspathPyImporter$ClasspathPyImporter_find_module_exposer.__call__(Unknown Source)
at org.python.core.PyBuiltinMethodNarrow.__call__(PyBuiltinMethodNarrow.java:48)
at org.python.core.imp.find_module(imp.java:761)
at org.python.core.imp.import_next(imp.java:1158)
at org.python.core.imp.import_module_level(imp.java:1350)
at org.python.core.imp.importName(imp.java:1528)
at org.python.core.ImportFunction.__call__(__builtin__.java:1285)
at org.python.core.PyObject.__call__(PyObject.java:433)
at org.python.core.__builtin__.__import__(__builtin__.java:1232)
at org.python.core.imp.importOneAs(imp.java:1564)
at org.python.pycode._pyx0.f$0(<string>:1)
at org.python.pycode._pyx0.call_function(<string>)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1687)
at org.python.core.Py.exec(Py.java:1731)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:268)
at com.so.Script.execute(Script.java:20)
Here's all I'm doing in my code (I'm actually invoking this via a Swing action on a JMenuItem calling new Script().execute(), which is most likely irrelevant):
package com.so;
import org.python.core.PyDictionary;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;
public class Script {
public Script() {
}
public void execute() {
PyDictionary table = new PyDictionary();
PySystemState state = new PySystemState();
PythonInterpreter interp = new PythonInterpreter(table, state);
String script;
script = "" +
"import com.foo.Bar as Bar\n" +
"";
interp.exec(script);
}
}
It doesn't even matter that there is no such package/class in my classpath. But what baffles me the most is that when I, thinking this has to be classpath related, created a separate mock project with the exact same classpath (same JAR files from the same locations on disk), the other project works just fine when run and it executes the actual script.
What could I be doing wrong here?
This happens with Java 1.8u241 (x64) and both jython-standalone-2.7.2.jar and an earlier 2.7.1 version. The ClassLoader in the stack trace is attempting to resolve "com".
I found the culprit entirely by accident.
My broken project used an older (ancient) version of the Glazed Lists library, namely glazedlists-1.8.0_java15.jar. This JAR seems to be directly incompatible with jython-standalone-2.7.2.jar. As soon as you put them on the same classpath and attempt to execute a python script, which imports any Java package starting with "com.", you end up with an InvalidJarIndexException. Updating to a newer version of said JAR resolved the issue.
Therefore, if you encounter a similar exception trying to run Jython, I suggest you update all dependencies of your project to latest or newer versions of them. In fact this would probably be the solution, even if not using Jython at all.
I am new to Java and Docker, so this may be very simple.
The program reads user input and passes it to a function that does a dfs for broken links with a depth limit.
import java.util.Scanner;
public class CrawlerTest {
public static void main(String [ ] args)
{
Scanner reader = new Scanner(System.in);
System.out.println("Enter full website url to crawl, starting with http://");
String domain = reader.next();
System.out.println("Enter max crawl depth: ");
int maxDepth = reader.nextInt();
reader.close();
Crawler crawler = new Crawler();
crawler.crawl(domain, maxDepth);
}
}
and the Crawler class imports the following libraries
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Stack;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javafx.util.Pair;
I exported a runnable jar file in eclipse
I created a Docker file with contents below:
FROM openjdk:12-alpine
WORKDIR / ADD Test.jar Test.jar
EXPOSE 8080
CMD java -jar Test.jar
I built the docker image with docker image build .. This succeeds and I get docker image id
Next, I just run this image with docker run -it
I am prompted to enter user input, which I successfully do. Then on hitting Enter second time I see the following errors, which I don't see when just running jar file in console:
Add --attach to the docker run command.
Without --attach, there is no console for the Java program to use, so any use of System.in will fail.
JavaFX was removed from default Java distribution starting from JDK11. It needs to be added explicitly via Java module system. That's why you are getting NoClassDefFoundError for the Pair class. Either change Java version prior to 11 or remove Pair class to resolve the issue. You can also add the JavaFX module to your module path.
I'm using following package for calling Weka functions from within Matlab https://github.com/NicholasMcCarthy/wekalab
and my code is
close all; clear all; clc;
dbstop if error
%%
javaclasspath('C:\Program Files (x86)\Weka-3-8\weka.jar');
javaaddpath('C:\Users\PC\wekafiles\packages\imageFilters\imageFilters.jar');
%%
import weka.filters.*
import weka.filters.Filter.*
import weka.filters.unsupervised.instance.imagefilter.*
import weka.filters.unsupervised.instance.imagefilter.BinaryPatternsPyramidFilter.*
import weka.classifiers.Classifier.*
import weka.classifiers.functions.SMO.*
import weka.classifiers.Evaluation.*
import weka.core.Attribute.*
import weka.core.FastVector.*
import weka.core.Instances.*
import weka.core.DenseInstance.*
import weka.classifiers.Classifier.*
import weka.classifiers.Evaluation.*
import weka.core.converters.ArffLoader.*
import weka.filters.unsupervised.instance.imagefilter.*
import weka.core.converters.ConverterUtils.*;
D = wekaLoadData('E:\pro\program\selectedPics\character\test.arff', 'ARFF');
myFilter = wekaFilter('weka.filters.unsupervised.instance.imagefilter.BinaryPatternsPyramidFilter');
filteredData = wekaApplyFilter(D, myFilter);
when i use the default filters of weka,
myFilter = wekaFilter('weka.filters.unsupervised.attribute.Standardize');
it works fine but when i use the installed package of weka (imageFilters) it give me this error
Error using javaObject
No class weka.filters.unsupervised.instance.imagefilter.BinaryPatternsPyramidFilter can be located on the Java class path
image filter (package) path: C:\Users\PC\wekafiles\packages\imageFilters
weka path: C:\Program Files (x86)\Weka-3-8
Environment variable & their paths:
CLASSPATH
C:\Program Files (x86)\Weka-3-8\weka.jar;
C:\Program Files(x86)\Weka-3-8\imageFilters\imageFilters.jar;
C:\Users\PC\wekafiles\packages\imageFilters\src\main\java;
C:\Users\PC\wekafiles\packages\imageFilters\src\main\java\filters\unsupervised\instance\imagefilter;
C:\Users\PC\wekafiles\packages\imageFilters\src\main\java\filters\unsupervised\instance;
java
C:\Program Files\Java\jre1.8.0_181\bin;
PATH
C:\Program Files\Java\jdk1.8.0_181\bin;
PATH_HOME
C:\Program Files\Java\jdk1.8.0_181;
You can use weka.Run in order to use additional packages in weka (weka.Run details)
On the Terminal:
Before using it you may want to add your weka.jar file into CLASSPATH:
export CLASSPATH=path_to_weka.jar
For example, a way to use this command:
java weka.Run weka.filters.unsupervised.instance.imagefilter.BinaryPatternsPyramidFilter -D "directory were files to filter are located" -i "input arff" -o "output arff"
For more info, check: Weka official documenation
In my case, I used the manual for Weka 3-7-8. The information mentioned is on the page 26 under Running installed learning algorithms. This could change from version to version.
Here you have a list of any manual you could need: Weka manuals for every version
About using it on Java, I don't have information about it. I wanted to answer this question because there might be people out there needing help with command line Weka.