【问题标题】:External python application is not running外部 python 应用程序未运行
【发布时间】:2014-03-15 08:53:44
【问题描述】:

您好,我只是创建了一个 java 应用程序来在外部运行我的 python 代码。但它想给我输出。 这是我的java代码:-

    package com.epatient;

import java.io.*;


public class InterpreterExample {

    //static String workingDir = System.getProperty("user.dir");
     //static String appDir =  workingDir + "\\epatient\\epatient_prediction.py";

     public static void main(String a[]){
         try{
             String appDir = "C:\\Users\\Thushara Kasun\\Documents\\juno\\serial.port\\epatient\\epatient_prediction.py";
              System.out.println(appDir);
         ProcessBuilder pb = new ProcessBuilder("python",appDir);
         Process p = pb.start();

         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
         String ret = in.readLine();
         System.out.println("value is : "+ret);
         }catch(NumberFormatException e){e.printStackTrace();}
         catch (IOException e) {e.printStackTrace();}        
          }
       }

这是我的python代码:-

    import sys
    from sklearn.externals import joblib
    import pandas as pd
    import numpy as np
    import csv

    from sklearn.decomposition import PCA

    import re
    import psycopg2

    import datetime
    import time

    con = None
    bio_data = None
    heart_rate = None
    so2_data = None
    temp_data = None
    bp_data = None

    try:

        con = psycopg2.connect(database='Epatient_user_1', user='postgres', password='root') 
        cur = con.cursor()

    ...#database access codes omited

        model_name = 'trained_model'
        est = joblib.load(model_name)
        predictions = est.predict(data)
        #predictions

    # <codecell>


    #sys.stdout.write(str(int(predictions[0])))
    #sys.stdout.flush()
    #print int(predictions[0])
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    #print st
    cur.execute('INSERT INTO patient_status (STATUS,TIME,PROCESSED) VALUES (' + str(int(predictions[0])) + ',\''+ st + '\',0);')
    con.commit()


except psycopg2.DatabaseError, e:
    print 'Error %s' % e    
    sys.exit(1)


finally:

    if con:
        con.close()

    sys.stdout.write(str(int(predictions[0])))
    sys.stdout.flush()
    #print int(predictions[0])

Java 输出只是 value is : null python代码没有问题,它运行良好。我只想打印一个字符串格式的布尔值。同时,我需要在本地 postgresql 数据库中更新该值(通过单独工作的 python 代码)。只是我的java应用程序没有执行python代码。请帮助我或给我另一种解决此问题的方法。

【问题讨论】:

  • 你能发布错误信息吗?
  • 没有错误信息,它只是打印输出 value is : null python 代码没有执行。如果它执行数据库应该更新。

标签: java postgresql python-2.7 bufferedreader processbuilder


【解决方案1】:

我认为您的 Java 代码还可以,然后尝试更改您的 Python 脚本。

请您将print int(predictions[0]) 更改为:

sys.stdout.write(str(predictions[0]))
sys.stdout.flush()

sys.stdout 将数据打印到控制台输出流

只是为了测试,将脚本的绝对路径放在 ProcessBuilder 构造函数中:

String appDir = "C:\\my\\full\\path\\to\\script.py";
ProcessBuilder pb = 
   new ProcessBuilder("python", appDir);

Process p = pb.start();
....

【讨论】:

  • 不工作 Lus。甚至python代码都没有运行。它应该更新 postgresql 数据库,但它没有得到更新。当我单独运行 python 程序时,它的工作。我需要在运行 java 代码时调用 python 代码。
  • 你试过放绝对路径吗?查看编辑
  • 你的意思是 appDir 对吧?没有朋友还是同样的错误信息。 ProcessBuilder pb = new ProcessBuilder("python",appDir);
猜你喜欢
  • 2016-08-14
  • 2018-04-17
  • 1970-01-01
  • 2011-11-03
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多