I folks:
I know that CGI is jurassic and before all of you call me lunatic, I must say that this question is only for EDUCATIONAL PURPOSES (in real cases I use JSP).
I'm trying to code a "Hello World" CGI in Java, and I'm unsucessful.
So I try the same job in C#.
Both programs (C# and Java) are totally equal (line by line). The C# works and the Java don't.
Here's the code in C#:
namespace CGI_CSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Content-Type: text/html\n"); // the extra "\n" is needed
Console.WriteLine("<html>");
Console.WriteLine("<head>");
Console.WriteLine("<title>CGI - C#</title>");
Console.WriteLine("</head>");
Console.WriteLine("<body>");
Console.WriteLine("<h1>Hello World !</h1>");
Console.WriteLine("</body>");
Console.WriteLine("<html>");
}
}
}
In the browser URL I wrote: http://localhost/CGI_CSharp.exe and BINGO! The C# code WORKS !
Now the same code in Java:
public class CGI_Java
{
public static void main(String[] args)
{
System.out.println ("Content-Type: text/html\n");
System.out.println ("<html>");
System.out.println ("<head>");
System.out.println ("<title>CGI - Java</title>");
System.out.println ("</head>");
System.out.println ("<body>");
System.out.println ("<h1>Hello World !</h1>");
System.out.println ("</body>");
System.out.println ("</html>");
}
}
Now I've tried the URL:
http://localhost/java.exe%20CGI_Java
(as you know, the %20 is the space => (http://localhost/java.exe CGI_Java)
I get:
HTTP 404.0 - Not Found
So, I try again, now with a batch file (CGI_Java.bat) with a single line of text:
java.exe CGI_Java (content of the CGI_Java.bat)
And now, I try the URL:
http://localhost&/CGI_Java.bat
Now the browser shows:
C:\inetpub\wwwroot>java.exe CGI_Java
This is the prompt followed by the command I wrote in the batch file.
Can someone help me?
Thanks in advance.
What server are you using?
Is java.exe on the path for the server?
(Note that if you alter the environment variable for the path you will have to restart the server for it to pick up the changes).
Related
This is a simple Java application which displays the default code page on Windows:
package doscommand;
import java.io.IOException;
import java.io.InputStream;
public class DosCommand {
public static void main(String[] args) throws IOException {
InputStream in = Runtime.getRuntime().exec("chcp.com").getInputStream();
int ch;
StringBuilder chcpResponse = new StringBuilder();
while ((ch = in.read()) != -1) {
chcpResponse.append((char) ch);
}
System.out.println(chcpResponse); // For example: "Active code page: 437"
}
}
On my Windows 10 machine this application always displays "Active code page: 437" because Cp437 is the default, and Runtime.getRuntime().exec() starts a new Process when running chcp.com.
Is it possible to create a Java application which instead displays the currently active code page for the existing Command Prompt window in which the code is running?
I want to be able to do something like this from the Command Prompt:
chcp 1252
java -jar "D:\NB82\DosCommand\dist\DosCommand.jar" REM Shows current code page is "1252".
chcp 850
java -jar "D:\NB82\DosCommand\dist\DosCommand.jar" REM Shows current code page is "850".
How do you specify a Java file.encoding value consistent with the underlying Windows code page? asked a similar question, though in that case the OP was seeking a non-Java solution.
I'd prefer a Java-only solution, but as alternatives:
Can this possibly be done using JNI, by calling some C/C++/C# code with access to the Windows API? The called code need only return a numeric value for the active code page.
I'll accept an answer which persuasively argues that it can't be done.
The solution turned out to be just one line of code. Using JNA, the value returned by the Windows API function GetConsoleCP() gives the console's active code page:
import com.sun.jna.platform.win32.Kernel32;
public class JnaActiveCodePage {
public static void main(String[] args) {
System.out.println("" + JnaActiveCodePage.getActiveInputCodePage());
}
/**
* Calls the Windows function GetConsoleCP() to get the active code page using JNA.
* "jna.jar" and "jna-platform.jar" must be on the classpath.
*
* #return the code page number.
*/
public static int getActiveInputCodePage() {
return Kernel32.INSTANCE.GetConsoleCP();
}
}
I need the functionality like that of the rsync linux tool in my Java program. For that, I chose the rsync4j library.
Using their documentation, I wrote the following program:
import com.github.fracpete.processoutput4j.output.ConsoleOutputProcessOutput;
import com.github.fracpete.rsync4j.RSync;
public class MainClass {
public static void main(String [] args) {
System.out.println("Started");//check
RSync rsync = new RSync()
.source("/home/arth/DataSourceFolder/a.txt")
.destination("/home/arth/DataDestinationFolder/")
.recursive(true);
// or if you prefer using commandline options:
// rsync.setOptions(new String[]{"-r", "/one/place/", "/other/place/"});
CollectingProcessOutput output = null;
try {
System.out.println("Inside try");
output = rsync.execute();
System.out.println("End of try");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(output.getStdOut());
System.out.println("Exit code: " + output.getExitCode());
if (output.getExitCode() > 0)
System.err.println(output.getStdErr());
}
}
In the snippet, in out local machine, a file a.txt is copied from one location to another. This works perfectly. The file is successfully copied when I run it and here is the output:
Started
Inside try
End of try
Exit code: 0
But my need is to sync a local directory with a directory lying at a remote host/machine. When I tried to do it using a simple rsync command from a terminal using the following command
rsync remoteUserName#23.24.25.244:/home/beth/remoteFolder/a.png /home/arth/DataSourceFolder
it works like a charm. a.png IS copied to local machine at path specified, although a password of remote machine is asked first.
But the problem when I use the above Java program to do the same operation, by replacing line # 11 and 12 by:
.source("remoteUserName#23.24.25.244:/home/beth/remoteFolder/a.png")
.destination("/home/arth/DataDestinationFolder/")
the program gets stuck after printing Started in the console. Neither an exception is thrown nor does the program proceed.
The question is that how do I fix this problem?
(old post, I know, but here it goes...) The rsync4j library does not allow interaction. In your case, the underlying rysnc binary prompts for a password in the process that the Java library created, but never receives one.
Starting with release 3.2.3-7, you can supply an instance of the sshpass wrapper to feed in the password (see this comment for an example).
Is it still possible to use Java in Classic ASP (IIS 7.* / Windows) via Java Moniker as is demonstrated at http://cephas.net/blog/2004/03/15/scripting-in-asp-with-java/? (and http://thrysoee.dk/InsideCOM+/ch11f.htm and https://hq.lojcomm.com.br/java/Interop.class.asp).
I cannot make it work from IIS 8.5 (or from dektop VbScript) in my Windows 10 with Java 1.8.0_131 installed in C:\Program Files\Java.
I get all the time: error'800401e4' with no other clarifications in the first line:
set Java_Date = getObject("java:java.util.Date")
response.write( Java_Date.toString() )
and the same error comes if I try to use NetBeans-compiled custom class:
set Java_Greeter = getObject("java:omatestit.Hello")
public class Hello
{
public static void main(String[] args)
{
//do nothing - this will keep us from getting a compile error
}
public String SayHello()
{
return "Hello Geek";
}
}
which locates in:
"C:\Program Files\Java\jdk1.8.0_131\include\win32\TrustLib\omatestit\Hello.class"
Can anyone figure out what is wrong with previous settings?
I have the following class in Java which prints "Hello World" in portuguese:
public class PrintUnicode {
public static void main(String[] args) {
System.out.println("Olá Mundo!");
}
}
I am using Eclipse, so I exported the project to a Runnable Jar File. After that, I went to cmd (Windows 7) and ran the generated jar file.
The result was:
Olß Mundo!
Is there an easy way to avoid this error?
Found the solution. Just change to:
public class PrintUnicode {
public static void main(String[] args) {
System.console().printf("Olá Mundo!");
}
}
The error with System.out happens because:
By default, Java encodes Strings sent
to System.out in the default code
page. On Windows XP, this means a
lossy conversion to an "ANSI" code
page. This is unfortunate, because the
Windows Command Prompt (cmd.exe) can
read and write Unicode characters. (source here)
This is my first post on the forum, hope all of you guys are well.
I've got a issue using JiST/SWANS, the ad hoc simulator in java within eclipse.
I managed to load the API, (as an external JAR ofcourse) but Im basically having a problem integrating the runtime of JiST within eclipse.
After running the hello world im usually getting a stackoverflowerror exception, since it may need modifications within the runtime.
import jist.runtime.JistAPI;
public class hello implements JistAPI.Entity {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("simulation start");
hello t = new hello();
t.myEvent();
}
public void myEvent()
{
JistAPI.sleep(1);
myEvent();
System.out.println("hello world, t=" + JistAPI.getTime());
}
}
the website is: http://jist.ece.cornell.edu/index.html
Thank you in advance!
Actually you need to run Main.java within jist.runtime. But before rigth click Main.java, properties, Run/Debug settings, New,Arguments and type your class name (plain name no .java needed) in Progam arguments. This will tell the jist interface to translate your code using the rewriter and run it.
Examples:
To run hello.java type "hello"
To run aodvsim.java type: "jist.swans.Main driver.aodvsim"
If there are arguments needed type them after the clas name like: "jist.swans.Main driver.aodvsim -n 25 -f 2000x2000 -a grid:5x5 -t 10,600,60"
Wilmer Arellano
How well does SWANS work? Given that the documentation and code date back to 2005, I am not sure if this is the best platform to use.