使用参数从java运行Python程序
在为学校做演示时,我编写了java部分,其他人编写了python,我不知道 我有一个java应用程序,它使用JavaFX打开一个窗口询问信息,然后该窗口应该获取信息并将其提供给python程序使用 其基本思想是,该程序将获取电话号码、提供商以及他们想要的信息类型,并将其发送给他们 我不知道如何通过java运行python程序,也不知道在运行时如何传递数据 爪哇:使用参数从java运行Python程序,java,python,runtime,Java,Python,Runtime,在为学校做演示时,我编写了java部分,其他人编写了python,我不知道 我有一个java应用程序,它使用JavaFX打开一个窗口询问信息,然后该窗口应该获取信息并将其提供给python程序使用 其基本思想是,该程序将获取电话号码、提供商以及他们想要的信息类型,并将其发送给他们 我不知道如何通过java运行python程序,也不知道在运行时如何传递数据 爪哇: package showcase.program; import javafx.application.Application;
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);
}
}
}
但是我不知道怎么做那部分
谢谢 试试看:
ProcessBuilder pb = new ProcessBuilder("python",pythonLocation,""+PhoneNum,""+
Carrier,""+Msg);
Process p = pb.start();
欢迎来到StackOverflow。请尝试使用适当的缩进再次发布代码。将代码粘贴到窗口中,选择它,然后单击
{}
图标。每行将向右移动4个空格。粘贴时避免使用制表符,或者每个制表符使用4个空格。好的,刚刚修复。好的,谢谢。现在请试着把所有不必要的东西都去掉,让它成为一个好主意。这有助于将我们的注意力集中在这个问题上,并经常帮助您发现问题。pythonLocation的格式是:“C:\pythonprogram\text\u automation”
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();