JythonScript is a simple Jython wrapper for easier execution and/or evaluation of Python expressions and scripts at runtime for JVM-based languages.
<dependency>
<groupId>com.github.adchilds</groupId>
<artifactId>jythonscript</artifactId>
<version>2.0.1</version>
</dependency>
None. JythonScript packages the Jython standalone JAR as part of it's distribution, so no further dependencies are required.
test.py:
from java.lang import System
def test():
System.out.println('Hello from Jython')
if __name__ == '__main__':
test()
Test.java:
import com.github.adchilds.jython.JythonScript;
public class Test {
public static void main(String[] args) {
String filePath = ClassLoader.getSystemResource("test.py").getPath();
JythonScript.execute(filePath);
}
}
Output: "Hello from Jython"
test2.py:
import sys
def multiply(a, b):
return a * b
if __name__ == '__main__':
a = sys.argv[1]
b = sys.argv[2]
result = multiply(a, b)
Test2.java
import com.github.adchilds.jython.JythonScript;
public class Test2 {
public static void main(String[] args) {
String filePath = ClassLoader.getSystemResource("test2.py").getPath();
Integer result = JythonScript.evaluate(filePath, 5, 5);
System.out.println("Result = [" + result + "]");
}
}
Output: "Result = [25]"
Test3.java
import com.github.adchilds.jython.JythonScript;
public class Test2 {
public static void main(String[] args) {
String script = "from java.lang import System\n" +
"\t\n" +
"\tdef test():\n" +
"\t\tSystem.out.println('Hello from Jython')\n" +
"\t\n" +
"\tif __name__ == '__main__':\n" +
"\t\ttest()";
PyCode compiledScript = JythonScript.compileString(script);
JythonScript.execute(compiledScript);
}
}
Output: "Hello from Jython"
- JythonScript - https://github.com/adchilds/JythonScript
- Python - https://www.python.org
- Jython - http://www.jython.org
jythonscript's People
Forkers
jasamuelsjythonscript's Issues
Can't load the python script paths?
When I used //String filePath = ClassLoader.getSystemResource("test2.py").getPath();// and created a python file named test2.py in the same directory, program shows a nullpointer exception in this line! what to do?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.