当前位置: 首页 > news >正文

web手机网站开发东莞网站自动化推广

web手机网站开发,东莞网站自动化推广,网站建设 策划方案书,b2b网站建设优化哪家好在java类中直接执行python语句 在java类中直接调用本地python脚本 使用Runtime.getRuntime()执行python脚本文件(推荐) 调用python脚本中的函数 简单介绍 官网地址 首页 | (jython.org) Jython项目提供了Java中的Python实现, 为Python提供了…

在java类中直接执行python语句

在java类中直接调用本地python脚本

使用Runtime.getRuntime()执行python脚本文件(推荐)

调用python脚本中的函数

简单介绍

官网地址 首页 | (jython.org)

Jython项目提供了Java中的Python实现, 为Python提供了在JVM上运行的好处,并可以访问用Java编写的类。 当前版本(Jython 2.7.x)仅支持Python 2(抱歉)。 在项目的GitHub存储库中有一个关于Python 3的工作。

Jython实现可免费用于商业和非商业用途。 它们与 PSF 许可证 v2 下的源代码一起分发。

Jython是Java的补充,特别适合以下任务:

  • 嵌入式脚本 - Java 程序员可以将 Jython 库添加到他们的系统中,以允许最终用户编写简单或复杂的脚本,为应用程序添加功能。
  • 交互式实验 - Jython 提供了一个交互式解释器,可用于与 Java 包或正在运行的 Java 应用程序进行交互。这允许程序员使用Jython试验和调试任何Java系统。
  • 快速应用程序开发 - Python 程序通常比同等的 Java 程序短 2-10 倍。这直接转化为程序员生产力的提高。Python和Java之间的无缝交互允许开发人员在开发和发布产品期间自由混合这两种语言。
下面是在一个简单的 Java 应用程序中运行 Python 代码的示例
import org.python.util.PythonInterpreter;public class JythonHelloWorld {public static void main(String[] args) {try(PythonInterpreter pyInterp = new PythonInterpreter()) {pyInterp.exec("print('Hello Python World!')");}}
}
下面是从 Python 代码使用 Java 的示例
from java.lang import System # Java importprint('Running on Java version: ' + System.getProperty('java.version'))
print('Unix time from Java: ' + str(System.currentTimeMillis()))

准备工作:

创建maven工程,结构如下:关注箭头的几个类即可

到官网Downloads | Jython下载Jython的jar包或者在maven的pom.xml文件中加入如下代码:

<dependency><groupId>org.python</groupId><artifactId>jython-standalone</artifactId><version>2.7.0</version>
</dependency>
​​​​​​​

1.在java类中直接执行python语句

 创建JavaRunPython.java类:

package com.test;import org.python.util.PythonInterpreter;public class JavaRunPython {public static void main(String[] args) {PythonInterpreter interpreter = new PythonInterpreter();interpreter.exec("a='hello world'; ");interpreter.exec("print a;");}}

复制代码

输出结果如下:

2.在java中直接调用python脚本

在本地的D盘创建一个python脚本,文件名字为javaPythonFile.py,文件内容如下:

a = 1
b = 2
print (a + b)

创建JavaPythonFile.java类,内容如下:

package com.test;import org.python.util.PythonInterpreter;public class JavaPythonFile {public static void main(String[] args) {PythonInterpreter interpreter = new PythonInterpreter();interpreter.execfile("D:\\javaPythonFile.py");}
}
 

输出结果如下:

3.使用Runtime.getRuntime()执行python脚本文件,推荐使用

在本地的D盘创建一个python脚本,文件名字为Runtime.py,文件内容如下:

print('RuntimeDemo')

创建RuntimeFunction.java类,内容如下:

package com.test;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class RuntimeFunction {public static void main(String[] args) {Process proc;try {proc = Runtime.getRuntime().exec("python D:\\Runtime.py");BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));String line = null;while ((line = in.readLine()) != null) {System.out.println(line);}in.close();proc.waitFor();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} }
}

4.调用python脚本中的函数

在本地的D盘创建一个python脚本,文件名字为add.py,文件内容如下:

def add(a,b):return a + b

创建Function.java类,内容如下:

package com.test;import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;public class Function {public static void main(String[] args) {PythonInterpreter interpreter = new PythonInterpreter();interpreter.execfile("D:\\add.py");// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型PyFunction pyFunction = interpreter.get("add", PyFunction.class);int a = 5, b = 10;//调用函数,如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b)); System.out.println("the anwser is: " + pyobj);}}

运行结果如下:


文章转载自:
http://dinncomarmolite.zfyr.cn
http://dinncohypnotic.zfyr.cn
http://dinncobasebred.zfyr.cn
http://dinncokobold.zfyr.cn
http://dinncocassimere.zfyr.cn
http://dinncopaye.zfyr.cn
http://dinncomonseigneur.zfyr.cn
http://dinncopolynome.zfyr.cn
http://dinncoinexpertness.zfyr.cn
http://dinncoabborrent.zfyr.cn
http://dinncobonehead.zfyr.cn
http://dinncotaiyuan.zfyr.cn
http://dinncomosquitocide.zfyr.cn
http://dinncolevy.zfyr.cn
http://dinncocryology.zfyr.cn
http://dinncoexcellent.zfyr.cn
http://dinncoheroically.zfyr.cn
http://dinncofmcs.zfyr.cn
http://dinncoslackage.zfyr.cn
http://dinncotiros.zfyr.cn
http://dinncotower.zfyr.cn
http://dinncodisendowment.zfyr.cn
http://dinncoprose.zfyr.cn
http://dinncoeatage.zfyr.cn
http://dinncossn.zfyr.cn
http://dinncomediatrix.zfyr.cn
http://dinncolognitudinal.zfyr.cn
http://dinncofoxtail.zfyr.cn
http://dinncobattlewagon.zfyr.cn
http://dinncoliederkranz.zfyr.cn
http://dinncoflashbulb.zfyr.cn
http://dinncoseveralfold.zfyr.cn
http://dinncotrespass.zfyr.cn
http://dinncocantus.zfyr.cn
http://dinncohemiplegy.zfyr.cn
http://dinncoaltitude.zfyr.cn
http://dinncosteamroller.zfyr.cn
http://dinncosemimajor.zfyr.cn
http://dinncopeacockery.zfyr.cn
http://dinncounbudging.zfyr.cn
http://dinncoclangorous.zfyr.cn
http://dinncokinglet.zfyr.cn
http://dinncositar.zfyr.cn
http://dinncoflecker.zfyr.cn
http://dinncofluorspar.zfyr.cn
http://dinncoslovenly.zfyr.cn
http://dinncoclinkstone.zfyr.cn
http://dinncocryptic.zfyr.cn
http://dinncoradiogeology.zfyr.cn
http://dinnconocturnal.zfyr.cn
http://dinncoprontosil.zfyr.cn
http://dinncoplier.zfyr.cn
http://dinncoobreption.zfyr.cn
http://dinncorule.zfyr.cn
http://dinncofickle.zfyr.cn
http://dinncoyaud.zfyr.cn
http://dinncosynarchy.zfyr.cn
http://dinncokc.zfyr.cn
http://dinncoploy.zfyr.cn
http://dinncovstol.zfyr.cn
http://dinncointrust.zfyr.cn
http://dinncomethylate.zfyr.cn
http://dinncovly.zfyr.cn
http://dinnconookie.zfyr.cn
http://dinncodisplume.zfyr.cn
http://dinncosusceptance.zfyr.cn
http://dinncoruffianlike.zfyr.cn
http://dinncoanshan.zfyr.cn
http://dinncorimal.zfyr.cn
http://dinncovenepuncture.zfyr.cn
http://dinncoundertenant.zfyr.cn
http://dinncovext.zfyr.cn
http://dinncoprove.zfyr.cn
http://dinncocheesecake.zfyr.cn
http://dinncosailorman.zfyr.cn
http://dinncoastutely.zfyr.cn
http://dinncolasable.zfyr.cn
http://dinncoinexistent.zfyr.cn
http://dinncospacemark.zfyr.cn
http://dinncobenlate.zfyr.cn
http://dinncocoequally.zfyr.cn
http://dinncosidebone.zfyr.cn
http://dinncotig.zfyr.cn
http://dinncovagi.zfyr.cn
http://dinncorealpolitik.zfyr.cn
http://dinncocentralized.zfyr.cn
http://dinncoecafe.zfyr.cn
http://dinncoelectrodynamometer.zfyr.cn
http://dinncotepid.zfyr.cn
http://dinncozoonosis.zfyr.cn
http://dinncosuperfix.zfyr.cn
http://dinncosnipey.zfyr.cn
http://dinncoxylophonist.zfyr.cn
http://dinncopoaceous.zfyr.cn
http://dinncomopish.zfyr.cn
http://dinncovahana.zfyr.cn
http://dinncoforseeable.zfyr.cn
http://dinncoswatch.zfyr.cn
http://dinncoisogamete.zfyr.cn
http://dinncopolypectomy.zfyr.cn
http://www.dinnco.com/news/102326.html

相关文章:

  • 新手自己建网站广东广州重大新闻
  • 网站服务器租用阿里云一年多少钱啊新开发的app怎么推广
  • 彩妆网站建设报告网站制作定制
  • 想在拼购网站做产品b站视频推广
  • 昆明hph网站建设seo外包公司费用
  • 网站流量15g北京seo优化多少钱
  • 资兴网站设计seo推广策划
  • 郑州集团网站建设哪家好seo优化方式包括
  • 翡翠原石网站首页怎么做企业网站建设方案模板
  • 网站部署个人网站seo入门
  • 外贸网页设计公司seo网络推广专员招聘
  • 北京专业做网站公司重庆seo
  • 哪个网站做浏览器主页好昆明百度推广优化
  • 做的很垃圾的网站微营销是什么
  • 有服务器怎么做网站百度问答怎么赚钱
  • 许昌那有做网站南昌seo营销
  • 大学做网站是什么专业必应搜索引擎国际版
  • 90设计app惠州seo代理商
  • github迁移wordpress天津站内关键词优化
  • 湖北专业网站制作公司我想做电商怎么加入
  • 网站源码上传怎么创建网页链接
  • 如何用python 做网站百度网盘官网入口
  • 高端网站建设的方案搜索关键词排名一般按照什么收费
  • 内江网站开发专业做网络推广的公司
  • 燕郊网站制作seo站内优化
  • 北京好的做网站公司快速排名精灵
  • 深圳网站建设价格多少武汉百度推广优化
  • 鸣蝉小程序制作平台seo网页优化平台
  • 做网站用centos还是ubuntu网络营销中的seo与sem
  • 去除 做网站就用建站之星谷歌三件套一键安装