繁体   English   中英

使用参数从Java运行Python程序

[英]Running a Python program from java, with arguments

在学校的演示文稿上工作,我编码了Java部分,其他人做了我不知道的python。

我有一个Java应用程序,该应用程序使用JavaFX打开一个窗口以询问信息,然后该窗口应该获取信息并将其提供给python程序使用。

基本思想是该程序将接收电话号码,提供者以及他们想要什么样的消息,然后将其发送给他们。

我不知道如何通过java运行python程序,也不知道如何在运行时传递数据。

Java的:

package showcase.program;


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
//import com.google.common.primitives.Ints;

public class ShowcaseProgram extends Application {

   String PhoneNum;
   String CarrierNumStr;
   String ChoiceStr;
   final Button button = new Button ("Submit");
   final Label notification = new Label ();
   final Label notification2 = new Label ();
    @Override
   public void start(Stage stage) {

    Scene scene = new Scene(new Group(), 500, 500);
    final ComboBox CarriersCombo = new ComboBox();
        CarriersCombo.getItems().addAll(
            "Verison",
            "Sprint",
            "T-Mobile",
            "AT&T",
            "MetroPCS",
            "Boost Mobile",
            "Cricket Wireless",
            "Staight Talk",
            "Virgin Mobile"

        );
    final ComboBox ChoiceCombo = new ComboBox();
        ChoiceCombo.getItems().addAll(
            "Insult",
            "Compliment"
        );
     TextField PhoneNumIn = new TextField ();  
    PhoneNumIn.setText("Label");
    PhoneNumIn.clear();


         button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                int count = 0;
                if (ChoiceCombo.getValue() == null){
                notification.setText("You have not selected : insult or compliment"); 
                count++;
                }
                if (CarriersCombo.getValue() == null){
                notification.setText("You have not selected a Carrier!"); 
                count++;
                }
                if (PhoneNumIn.getText() ==  null){
                notification.setText("You have not entered a Phone number");
                count++;
                }
                if(PhoneNumIn.getText() == null){ //try to get google api imported properly for this to use Ints.tryParse()
                notification.setText("Please make sure your phone number is numeric only.");
                count++;
                }
                System.out.println(count);



                if (count == 0){
                    PhoneNum = PhoneNumIn.getText();
                    CarrierNumStr = CarriersCombo.getValue().toString();
                    ChoiceStr = ChoiceCombo.getValue().toString();
                    PhoneNumIn.setText(null);
                    CarriersCombo.setValue(null);
                    ChoiceCombo.setValue(null);
                    notification.setText("Thank you");
                } else {
                    notification2.setText("The information you inputed is incorrect, please try again.");
                }
           }     
         });







    GridPane grid = new GridPane();
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("Phone Number :"), 0, 0);
    grid.add(PhoneNumIn, 1, 0);
    grid.add(new Label("Carrier :"),0 , 2);
    grid.add(CarriersCombo, 1 , 2);
    grid.add(new Label("Choice :"), 0, 3);
    grid.add(ChoiceCombo, 1 , 3);
    grid.add(button, 1, 4);
    grid.add(notification, 1, 5);
    grid.add(notification2, 1, 6);
    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();
    }

    public static void launchPy(String PhoneNum, int Carrier, String Msg){

    }


    public static int CarrierToInt(String Carr){
        int x;
            if (Carr == "Verison"){
                x = 7;
            } else if (Carr == "Sprint"){
                x = 5;
            } else if(Carr == "T-Mobile") {
                x = 6;
            } else if(Carr == "AT&T") {
                x = 0;
            } else if(Carr == "MetroPCS") {
                x = 3;
            } else if(Carr == "Boost Mobile") {
                x = 1;
            } else if(Carr == "Cricket Wireless") {
                x = 2;
            } else if(Carr == "Straight Talk") {
                x = 4;
            } else if(Carr == "Virgin Mobile") {
                x = 8;
            }

            else {
                x = 0;
            }
        return x;
    }

    public static String Message(String ChoiceStr){
        int RNG;
        RNG = (int )(Math.random() * 1 + 3);
        String msg = "";
        String[] insults = new String[5];
        String[] compliments = new String[5];
        insults[1] = "Fart You";//preset insults and compliments go here
        insults[2] = "";
        insults[3] = "you stink!";
        compliments[1] = "Nice Hair";
        if (ChoiceStr == "Insult"){
            msg = insults[RNG];
        } else {
            msg = compliments[RNG];
        }
    return msg;
    }


    public static void main(String[] args) {
       while(true) {
           launch(args);

       }

    }

}

python程序设置为接受3个输入,如launchPy方法中提供的那样。

理想情况下,它将类似于(pseudo)

public static void launchPy(String PhoneNum, int Carrier, String Msg){
string pythonLocation = "/home/...";
//run python(pythonLocation, PhoneNum, Carrier, Msg)
}

但我不知道该怎么做。

谢谢!

    ProcessBuilder pb = new ProcessBuilder("python",pythonLocation,""+PhoneNum,""+
       Carrier,""+Msg);
    Process p = pb.start();

尝试:

Process process;   
String commands = "python ... "
ProcessBuilder pb = new ProcessBuilder();
pb.command(commands);
process = pb.start();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM