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)
Related
IDEA version: 2021.3.2
JAVA version: 1.8.0
The encoding format is UTF-8
I am a beginner in java, I try to install IEDA and run a sample code to output "HelloWorld", then get the error java: illegal character:'#' and other errors
What I tried:
Check the env path, right.
Create a txt file-> enter test code-> change the file type to .java, then run the cmd javac test.java and java xxx, it output HelloWorld
Change the encoding format to GBK and then back to UTF-8
Add -encoding UTF8 in IDEA settings
update1
public class test{
public static void main(String[] args) {
System.out.println("Just a test");
}
}
I'm Just trying to test Java Unicode support. I found that Java supports Unicode characters in their Class Names. But when I tried to use Unicode fonts It is not compiling. Below is the code
It Throws below error during Compilation
The character set of the File and Eclipse workspace is to UTF-8.
Update: Here is the Source. This has Unicode Tamil letters
public class தமிழ் {
private static String வணக்கம் = "வணக்கம்";
public static void main(String[] args) {
// TODO Auto-generated method stub
வணக்கம்சொல்();
}
private static void வணக்கம்சொல்() {
System.out.println(வணக்கம் + " வருக! வருக!!");
}
}
A quick demonstration about unicode characters in class names and the hassle on Windows.
Create following Java class file
Main.java
class Main {
public static void main(String...args) {
\u0ba4\u0bae\u0bbf\u0bb4\u0bcd.main(new String[0]);
}
}
class \u0ba4\u0bae\u0bbf\u0bb4\u0bcd {
public static void main(String[] arrstring) {
System.out.println("\u0bb5\u0ba3\u0b95\u0bcd\u0b95\u0bae\u0bcd unicode!");
}
}
All unicode characters are used with the unicode escape notation.
So actually following source would create the same class files
class Main {
public static void main(String...args) {
தமிழ்.main(new String[0]);
}
}
class தமிழ் {
public static void main(String[] args) {
System.out.println("வணக்கம் unicode!");
}
}
Compile the source (the one with the unicode escapes)
javac Main.java
this creates the class files Main.class and தமிழ்.class (you can check the file names e.g. with explorer . in the same directory)
in CMD console the unicode file name cannot be shown
> dir /b *.class
Main.class
?????.class
> java Main
??????? unicode!
in ConEmu the file name is displayed correctly
> dir /b *.class
Main.class
தமிழ்.class
> java Main
??????? unicode!
even the file name தமிழ்.class cannot be shown and accessed correctly in a CMD session, Java is able to execute the class. This means the class is stored correctly with the unicode characters. But the output is broken in both cases.
If you run the above code on a Linux machine the output will be as expected
$ java Main
வணக்கம் unicode!
edit the class with unicode characters can be executed on Linux directly
$ java தமிழ்
வணக்கம் unicode!
edit PowerShell ISE
PS > ls *.class
...
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 08/04/2018 12:34 317 Main.class
-a--- 08/04/2018 12:34 443 தமிழ்.class
PS > java Main
??????? unicode!
PS > java தமிழ்
java : Error: Could not find or load main class ?????
At line:1 char:1
+ java தமிழ்
edit Related to this bug report on Eclipse it seems it's working on Windows 10 (which I cannot verify, don't have one)
It is a matter of:
Unicode text normalisation: ĉ could be one Unicode code point (symbol) or two c and a combining diacritical mark ^ (zero-width). The operating system uses one of them. Ideally the IDE should enforce a canonical form. (No idea.)
Windows command line cmd.exe is restricted to its system encoding. However you could have a pure ASCII main class, calling the main of your class.
An executable jar file with an ASCII name should also pose no problem. The MANIFEST.MF is already in UTF-8, but as the line length should not exceed 72 bytes, and UTF-8 is multibyte per char, be careful.
Then there are version control systems that can make problems. Especially try switching between Windows and Linux.
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 made a simple JOptionPane class that should pop up with a string, when I type:
javac Hellodialog.java
nothing happens at all. No error messages but nothing comes up.
import javax.swing.JOptionPane;
public class Hellodialog
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "fsdfsdfdsfds");
}
}
compile into a java .class file
javac Hellodialog.java
run the java class
java Hellodialog
You use the javac command to compile your source to byte code, and the java command to run your compiled code.
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).