我想用Apache HTTP Server运行一个java程序。我很难在浏览器中显示简单的输出。
在我的cgi-bin文件夹中,我有CgiTest.java,如下所示:
private void setupDrawer(Toolbar toolbar) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
initAvailabilityChange(navigationView);
initMenuHeaderViews(navigationView);
}
private void initAvailabilityChange(NavigationView navigationView) {
switchItem = navigationView.getMenu().findItem(R.id.availability);
CompoundButton switchView = (CompoundButton) MenuItemCompat.getActionView(switchItem);
switchNew = (Switch) switchView.findViewById(R.id.switchLocation);
switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
isOnShift = isChecked;
presenter.onAvailabilityChange(isOnShift, this);
if (isChecked) {
switchItem.setTitle(R.string.availability_on_shift);
} else {
switchItem.setTitle(R.string.availability_off_shift);
}
});
}
private void initMenuHeaderViews(NavigationView navigationView) {
View header = navigationView.getHeaderView(0);
tvName = (TextView) header.findViewById(R.id.tvName);
tvRank = (TextView) header.findViewById(R.id.tvRank);
}
我得到的错误是:
#!"C:\Program Files\Java\jdk1.8.0_101\bin\java.exe"
public class CgiTest {
public static void main(String[] args) {
String type = "Content-Type: text/html\n\n";
String output = "<html>" +
"<p>Hi there </p>" +
"</html>";
System.out.println(type);
System.out.println(output);
}
}
如何运行我的java程序?如果我必须从某些脚本调用它怎么做?
答案 0 :(得分:2)
我做到了! 对于那些在这里找到这一天的人来说,这是一种方法:
步骤1. Java程序
public class CgiTest {
public static void main(String[] args) {
String type = "Content-Type: text/html\n\n";
String output = "<html>" +
"<p>Hi there </p>" +
"</html>";
System.out.println(type);
System.out.println(output);
}
}
步骤2.将其放入cgi-bin文件夹
步骤3.运行javac CgiTest.java
步骤4.安装cygwin
步骤5.在cgi-bin文件夹中创建invoker.cgi:
#!C:\cygwin64\bin\bash.exe
java -cp ./ CgiTest
步骤6.配置conf / httpd.conf并允许执行cgi脚本。确保你有这些行:
<Directory "c:/Apache24/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Options FollowSymLinks
Require all granted
</Directory>
AddHandler cgi-script .cgi
步骤7.在浏览器中运行URL。确保正确输入您的端口。我的是180,但你的是conf / httpd.conf
http://localhost:180/cgi-bin/invoker.cgi
这是在Windows中执行此操作的方法之一。