【问题标题】:Run CGI Java Program运行 CGI Java 程序
【发布时间】:2017-05-26 12:52:11
【问题描述】:

我想用 Apache HTTP Server 运行一个 java 程序。我正在努力在浏览器中显示简单的输出。

在我的 cgi-bin 文件夹中,我有 CgiTest.java,它看起来像这样:

#!"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);
}
}

我得到的错误是:

End of script output before headers: CgiTest.java
Error: Could not find or load main class C:.Apache24.cgi-bin.CgiTest.java\r: C:/Apache24/cgi-bin/CgiTest.java

如何运行我的java程序?如果我必须从某个脚本中调用它怎么办?

【问题讨论】:

    标签: java apache http web cgi


    【解决方案1】:

    我成功了! 对于那些发现这一天的人来说,这是一种方法:

    步骤 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);
    }
    }
    

    第二步,放入cgi-bin文件夹

    步骤 3. 运行 javac CgiTest.java

    步骤 4. 安装 cygwin

    Step 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 中执行此操作的方法之一。

    【讨论】:

      猜你喜欢
      • 2011-08-15
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 2014-09-16
      • 2011-08-05
      相关资源
      最近更新 更多