Java Classname with unicode character not Running - java

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.

Related

Java program runs yet compilation fails

I wrote a Java program whose filename was (intentionally) different from the class I wrote inside the file. The javac command failed as expected on both CMD and WSL. The java command however worked and ran my print statement. I wrote the code intentionally this way so there is no way it was a previously compiled version of the code. The following code was written in a file called "explainJava.java" (notice the filename is different from the class name).
public class explain{
public static void main(String[] args) {
System.out.println("Java is weird");
}
}
I've had to google this myself, but I think I've found an explanation in this article.
According to that source as of Java 11 java is capable of compiling a single source file into memory.
What I conclude from that: When the file is compiled into memory and not written to disk it obviously cannot have a file name. If there is no filename there is no such thing as a wrong filename, therefore the code executes.
Please also note that the restriction of having to name a file like the public class within that file is more of a design decision to make work for the compiler easier/ faster. It is not a physical restriction so to speak. Have a look at the following thread for more details.
If you put this code:
public class explain {
public static void main(String[] args) {
System.out.println("Java is weird");
}
}
into a file named explainJava.java, and then compile it with this:
javac explainJava.java
you will get an error that correctly informs you that your filename ("explainJava") and the class defined inside that file ("explain") do not match:
explainJava.java:1: error: class explain is public, should be declared in a file named explain.java
public class explain{
^
1 error
If you run this command:
$ java explainJava.java
Java is weird
you see expected output, because you're skipping the explicit compilation step (that is, you aren't running javac first) and instead relying on behavior introduced in Java 11 that allows you to compile+run in a single step. Here's an explanation: Does the 'java' command compile Java programs?
So the answer is to either:
rename your file to match the class, so change the filename to "explain.java", or
rename the class to match the file, change public class explain to be public class explainJava

IDEA run a sampe code and get the issue: java: illegal character:'#'

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

How to launch single-file programs in Java 11 (or later)?

JEP 330 describes a new feature in JDK 11 for launching single-file programs in Java. I've tried:
$ ./Example.java
but it doesn't work. What is the correct usage?
Short-version:
$ java Example.java data.txt
or (with #!):
$ ./example data.txt
Details:
Working example here.
Consider a single-file program to print the lines in a file:
import java.nio.file.*;
import java.util.stream.Stream;
public class ExampleJDK11 {
public static void main(String[] args) throws Exception {
// snip ... print file args[0]
}
}
Usage 1:
Assuming the code is in Example.java and that java is on the PATH, then usage is:
java Example.java data.txt
Note that there is no javac step (!)
Note that the filename need not match the classname.
Usage 2:
Assume the code is in a file, example, with a "shebang" line at the top:
#!/Users/measter/tools/jdk-11.jdk/Contents/Home/bin/java --source 8
import java.nio.file.*;
import java.util.stream.Stream;
// as above
Usage is:
./example data.txt
Though the answer by you includes correct information. Just trying to put this into simpler terms, a file can simply be executed using java from JDK11 onwards, for example on MacOS
.../jdk-11.jdk/Contents/Home/bin/java Sample.java
This would seek and execute the standard public static void main(String[] args) method. As one can notice(even beginners) that this method accepts args of type String, hence the arguments placed after the name of the source file in the original command line are passed to the compiled class when it is executed. Therefore the following command
.../jdk-11.jdk/Contents/Home/bin/java <file-name>.java arg1 arg2
would provide the string arguments arg1, arg2 during the execution phase.
Side note - If the file includes multiple classes with standard main methods, the first top-level class found in the source file which shall contain the declaration of the standard public static void main(String[]) method is executed.

Error: Could not find or load main class Hello2

I'm just learning java and following a book.
I have a program written via text editor and run commands via cmd.
I've complied 1 program thru javac and executed thru java no problem. (Hello)
Then I modified that to add a comment to the class, named file Hello2.java. I compiled it with no problems, but upon execution, I receive this error: Could not find or load main class Hello2.
I have classpath and path set correct;y on environment variables.
Ideas?
UPDATE
Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Hello2.java
//Filename Hello2.java
//Written by
//Written on
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*This class demonstrates the use of the println() method to print the message Hello, world! */
I've found the solution to my problem. I know it's not a code problem. But what I did is that I deleted CLASSPATH from system variables and everything now works...at least for now.
Thanks a lot everyone for your inputs, much appreciated!
You have to change the name of the public class too when you change the name of the file. So, if your file is called Hello2.java, the class should be called Hello2 and not Hello.
Are you sure you set the classpath correctly? Why don't you try running java -cp the directory of the .class file Hello ? If that doesn't work please upload the full stacktrace.
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath.
Note that the windows classpath separator is a semi-colon ie ;
If your class file is saved in following directory with Hello2 program name
d:\sample
java -cp d:\sample Hello2
java -cp . Hello2
I believe you have Hello2.java file as below.
class Hello {
public static void main (String args[]) {
System.out.println("Hello");
}
}
Change it to
class Hello2 {
public static void main (String args[]) {
System.out.println("Hello");
}
}
The change is class Hello2 instead of class Hello.
Note : You should always have classname and file name SAME.
Good Luck!!!
Update 1
Are you doing below steps??
Wrote Hello.java
Compile by javac Hello.java
Run by java Hello
Rename Hello.java to Hello2.java
Rename class name i.e. class Hello to class Hello2
javac Hello2.java
java Hello2
I believe you are missing Step 6 & executing step 7 after step 5. Please confirm.

Java unicode characters error in cmd

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)

Categories