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

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

Related

Java Classname with unicode character not Running

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.

Connecting java with constraint logic programming

i'm trying to connect java with constraint logic, i'm using netbeans for java and eclipse 6.1 for constraint logic, but when i'm trying to run the code there is an exception appears java.lang.IllegalArgumentException: Missing eclipse.directory property
i've used a tutorial that explain how to connect them, which says that After compilation, to run the program, start the Java interpreter as you normally would but before the name of the class, supply the command line option
-Declipse.directory=<eclipse_directory>
and i don't know where to place it in netbeans
here is the code
import com.parctechnologies.eclipse.*;
import java.io.*;
public class eclipseConnection {
public static void main(String[] args) throws Exception
{
try{
EclipseEngineOptions eclipseEngineOptions = new EclipseEngineOptions();
EclipseEngine eclipse;
eclipseEngineOptions.setUseQueues(false);
eclipse = EmbeddedEclipse.getInstance(eclipseEngineOptions);
eclipse.rpc("write(output, 'hello world'), flush(output)");
((EmbeddedEclipse) eclipse).destroy();
}catch(Exception e){
System.out.println(e);
}
}
}
You can add the property definition in the 'Run' menu: Run > Set Project Configuration > Customize.... Make sure you enter the property definition -Declipse.directory=<eclipse_directory> in the VM Options section.
Let's use the command line and the example source file Quicktest.java.
Copy the example:
copy "C:\Program Files\ECLiPSe 6.1\doc\examples\JavaInterface\Quicktest.java" .
Compile it:
javac -classpath "C:\Program Files\ECLiPSe 6.1\lib\eclipse.jar" QuickTest.java
Run it:
java -classpath ".;C:\Program Files\ECLiPSe 6.1\lib\eclipse.jar" -Declipse.directory="C:\Program Files\ECLiPSe 6.1" QuickTest
hello world

Run Java Program in Iseries(AS400)

Using Qshell,I have compiled Java Program that is saved in IFS Folder.After Compilation,Class file has been generated.But when i tried to run this class,it is not displaying any output.It simply gives $Prompt without any error.But when i checked the spool files,it is showing this:"Unable to Complete Java Program because of reason 04 and Code 4 means:Unable to find method id required to run java program.."
Ex. Simple Hello World Program :System.out.prinln("Hello World");
Compile:-cd /test(where test=directory where program is saved)
javac sample.java
Run:-java cp /test sample
The main method signature may not be valid. The code should be placed inside a properly defined main method:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
To compile and execute from QSH:
$ javac HelloWorld.java
$ java HelloWorld
Hello World
See Lesson: A Closer Look at the "Hello World!" Application for more information.

Issues with running a compiled java program

I am a Java beginner. I wrote a quintessential "Hello, World!" program. It compiles, but won't run. The terminal says there is an exception in the thread main, and that the class hello is not found. I am using Ubuntu 12.04. What could be wrong here?
The file is called hello.java.
The commands I used:
$javac hello.java
$java hello
My code is below:
class hello{
public static void main(String[] args) {
system.out.print("Hello");
}
}
EDIT-----------------------------------------------------------------------------------------
I just realized that I am using openjdk7. Does that pose a problem?
class must be: public class hello
system.out.print is wrong, must be: System.out.println("Hello World");
Is the filename hello or Hello? The only way I duplicated your problem was by having the class name wrong, and in java the class name an filename must exactly match (meaning the case too). So if your filename is Hello.java and the class name is hello the program will compile fine but throw the same error you mentioned. This is just a guess though.
Is that all your code? Since you use a terminal, can you add the results of the command ls -lR to your question? (run it in the same directory as your original command executing the application).

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