简体   繁体   中英

Run CGI Java Program

I want to run a java program with Apache HTTP Server. I'm struggling to show simple output in the browser.

In my cgi-bin folder I have CgiTest.java which looks like this:

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

The error I get is:

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

How to run my java program? If I have to invoke it from some script how to do it?

I made it work! For those of you who find this one day here is a way to do it:

Step 1. Java program

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

Step 2. Put it in cgi-bin folder

Step 3. Run javac CgiTest.java

Step 4. Install cygwin

Step 5. Create invoker.cgi in cgi-bin folder:

#!C:\cygwin64\bin\bash.exe
java -cp ./ CgiTest

Step 6. Configure conf/httpd.conf and Allow cgi scripts execution. Make sure you have those lines:

<Directory "c:/Apache24/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Options FollowSymLinks
Require all granted
</Directory>
AddHandler cgi-script .cgi

Step 7. Run the URL in your browser. Make sure you enter you port correctly. Mine is 180 but yours is in conf/httpd.conf

http://localhost:180/cgi-bin/invoker.cgi

This is one of the ways to do it in Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM