如何在PHP中使用Java代码?

huangapple go评论77阅读模式
英文:

How can I use Java codes in PHP?

问题

我有一个PHP代码和三个Java文件(在一个包中)。
我想在我的PHP代码中使用Java代码和变量。

我认为我应该首先像这样包含Java文件:

<?php include 'sample.java'; ?>

但我不确定这样是否正确,即使正确,我也不知道接下来该怎么做。

我在韩国网页上找到了一个旧的示例:

// HelloWorld.java
public class HelloWorld
{
String hw = "Hello World";


public String getHelloWorld()
{
return hw;
}
}

// HelloWorld.php
<?
$myObj = new Java("HelloWorld");
echo (string) $myObj->getHelloWorld();
?>

(我认为应该是<?php ?>而不是<? ?>,无论如何,它们都不起作用)
但它不起作用,文本显示在我的页面上:

// HelloWorld.java public class test { String hw = "Hello World"; public String getHelloWorld() { return hw; } } // HelloWorld.php

Fatal error: Uncaught Error: Class 'Java' not found in */*my directory*/* Stack trace: #0 {main} thrown in */*my directory*/*

我如何在PHP中使用我的Java代码?

英文:

I have a php code and java codes(exactly 3 java files in 1 package).
I want to use java codes and variables in my php code.

I think I should include java file first like this

&lt;?php include &#39;sample.java&#39;; ?&gt;

but I'm not sure about this and even if it's right, I don't know what to do next.

I found an old example in Korean web page :

// HelloWorld.java
public class HelloWorld
{
String hw = &quot;Hello World&quot;;


public String getHelloWorld()
{
return hw;
}
}

// HelloWorld.php
&lt;?
$myObj = new Java(&quot;HelloWorld&quot;);
echo (string) $myObj-&gt;getHelloWorld();
?&gt;

(I think it should be &lt;?php ?&gt; not &lt;? ?&gt; , anyway they didn't work both)
but it doesn't work and texts were shown on my page :

// HelloWorld.java public class test { String hw = "Hello World"; public String getHelloWorld() { return hw; } } // HelloWorld.php

Fatal error: Uncaught Error: Class 'Java' not found in /my directory/ Stack trace: #0 {main} thrown in /my directory/

How can I use my java codes in php?

答案1

得分: 0

你不能直接在你的PHP文件中包含Java类。要执行Java,你需要先使用javac编译Java代码,然后使用java执行生成的字节码。

这仍然是可行的,但过程会稍微复杂一些。

你需要编写你的Java程序,使其像一个命令行程序一样工作,你可以向它传递命令/开关,并根据你的参数调用某些方法。然后,你需要将已编译的类创建成一个JAR文件,并通过将主类添加到MANIFEST.MF使其成为可执行的JAR文件。

例如:

java -jar yourjar.jar hello

输出
Hello World

在PHP中,你可以使用shell_exec函数来调用它:

<?
shell_exec("java -jar /home/workspace/target/yourjar.jar hello");
?>
英文:

You can't directly include java classes in your php files. To execute java, you need to first compile java code using javac and then execute the generated bytecode using java.

It is still doable, but the process is bit more involved.

You need to write your java program to work like a command line program where you could pass commands/switches to it and based on your parameters, call some method. Then you would need to create a jar file out of your compiled classes and make it a executable jar by adding main class into MANIFEST-MF.

For example :

java -jar yourjar.jar hello

output :
Hello World

In PHP, you could use shell_exec function to call this

&lt;?
shell_exec(&quot;java -jar /home/workspace/target/yourjar.jar hello&quot;);
?&gt;

</details>



# 答案2
**得分**: 0

首先,Java和PHP具有不同的运行环境,因此您不能将Java文件包含到PHP中。但您可以将Java部分编译为一个独立的程序,并通过```exec()```和```system()```从PHP中执行它。但随之而来的问题是如何将数据从PHP传递给Java,以及将结果从Java传递回PHP。但听起来您的Java部分可能不是一个完整的程序,因此您可能需要学习两种语言。更好的做法是查看是否有一个库具有与您的Java文件相同的功能。

<details>
<summary>英文:</summary>

First of all java and php have different runtime environment so you can not include your java file to php. But you can compile your java part to an own programm and execute it from php by ```exec()``` ```system()```. But than you need to think about how to get data from php to java and the result back to php. But it sounds like your java part is not a complete program, so you would need to learn both laguages. Better is to look if you find a library which will have the same functionality than your java files

</details>



huangapple
  • 本文由 发表于 2020年8月6日 03:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63272574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定