首先,需要确保目的程序 ,php能独立执行。能输出。
纯粹只是因为用java写复杂逻辑代码比较蛋疼,或者说,因为有现成的代码是php的,重写需要花大量的时间。所以以java 的命令形式进行调用。
步骤 :
1.php代码一份
2.php运行环境一份
2.1 如果非指定形式调用。默认配置文件需要为 : php.ini ,不能有别名
2.2 配置内部内容需要清理不必要的配置,引用路径调整
2.3 单独执行一个文件,调整到不出问题,能正常执行为止
c:ooxxphp.exe “c:aabbindex.php”
3.用java Swing 绘制一个壳,用myeclipse ,前提需要有java 基础,或类似的winform基础比较容易上手,不然适当进行学习即可.
4.用exe4j 打包
参考:http://blog.martoo.cn/?p=613
补充相关代码:
一、命令调用
try {
String php_exe = System.getProperty("user.dir")
+ "/php/php/php.exe";
String php_file = System.getProperty("user.dir")
+ "/php/exec.php";
if (!filexists(php_exe)) {
JOptionPane.showMessageDialog(null,
"The php.exe file is not exist");
System.exit(0);
}
if (!filexists(php_file)) {
JOptionPane
.showMessageDialog(null, "The php file is not exist");
System.exit(0);
}
String shell = php_exe + " "" + php_file + """;
Process ps = Runtime.getRuntime().exec(shell);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(
ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
String contentempty = "";
while ((line = br.readLine()) != null) {
contentempty += line;
sb.append(line).append("n");
}
String result = sb.toString();
if (!contentempty.isEmpty()) {
recordinfo(result);
JOptionPane.showMessageDialog(null, result);
}
} catch (Exception e) {
recordinfo(e.getMessage());
}
public static void recordinfo(String log) {
String path = System.getProperty("user.dir") + "/php_log.txt";
try {
FileWriter fw = new FileWriter(path, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(log);
pw.close();
// bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean filexists(String filename) {
return new File(filename).exists();
}
输出信息可直接用于弹出框提示。
也可以记录为日志。
二、通讯操作
使用java properties
properties 保存和读取代码
Properties prop = new Properties();
FileInputStream fis;
FileOutputStream fos;
try {
fis = new FileInputStream(System.getProperty("user.dir")
+ "/php/prop.properties");
fos = new FileOutputStream(System.getProperty("user.dir")
+ "/php/prop.properties");
prop.load(fis);
prop.setProperty("csvpath", jfc.getSelectedFile()
.getAbsolutePath());
prop.save(fos, " config file");
// System.out.println(prop.getProperty("a"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
php 获取代码:
参考:http://blog.martoo.cn/?p=825
三:常用功能代码
文件选择框
JFileChooser jfc = new JFileChooser();
if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//获取路径
System.out.println(jfc.getSelectedFile().getAbsolutePath());
}
弹出提示框
JOptionPane .showMessageDialog(null, "The php file is not exist");
补充 : 意大利操作系统(win7 64x)关闭不了问题
Process ps;
try {
ps = Runtime.getRuntime().exec("taskkill /f /im xxx.exe");
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(
ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
String contentempty = "";
while ((line = br.readLine()) != null) {
contentempty += line;
sb.append(line).append("n");
}
String result = sb.toString();
if (!contentempty.isEmpty()) {
recordinfo(result);
JOptionPane.showMessageDialog(null, result);
}
} catch (IOException e2) {
e2.printStackTrace();
} catch (InterruptedException xe) {
xe.printStackTrace();
}
转发请注明出处http://blog.martoo.cn
如有漏缺,请联系我 QQ 243008827