使用参数从java运行Python程序

时间:2018-05-02 19:25:26

标签: java python runtime

为学校做演示,我编写了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程序设置为使用launchPy方法中提供的3个输入。

理想情况下,它会像(伪)

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

但我不知道该怎么做。

谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试:

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