Error compiling Java file with special characters in class name - java

I am encountering an issue compiling a source file with a special character in the class name. The class file compiles fine in the Eclipse IDE, but not from javac. I believe I need to leverage the -encoding flag, but haven't hit the right setting yet. I would appreciate any pointers:
File Name: DeptView和SDO.java
Java Source:
public interface DeptView\u548cSDO {
public int getDeptno();
public void setDeptno(int value);
}
Error Message:
Running javac *.java results in the following error message:
javac: file not found: DeptView?SDO.java
UPDATE
I am currently trying the compile at a Windows XP command prompt
Ultimately this compile will need to be part of an ant build, an run on different operating systems
I work on the tool that is producing this generated source file.

One solution is to list the file name of each compilation unit in a separate file, say files, and pass #files as a command-line argument to javac. Otherwise, you will have to set the locale of your shell, so that it is using the correct character encoding.

Have you tried using -encoding UTF8 or -encoding UTF16LE (Little Endian) or -encoding UTF16BE (big endian)? (The usage of LE or BE depends on the system you are using -- Windows is LE from what I remember.)

Related

VS Code Java System Output Encoding

I have a problem with encoding of Java System Output occurring only in Visual Studio Code.
My program is very simple:
public class Main {
public static void main(final String[] args) {
System.out.println("Příliš žluťoučký kůň úpěl ďábelské ódy");
}
}
but the output looks like this:
Příliš žluťoučký kůň úpěl ďábelské ódy�ábelské ódy�dy
My file has the UTF-8 encoding and it's being compiled with -Dfile.encoding=UTF-8 flag. I am using the official Java Extension Pack by Microsoft in Visual Studio Code.
as you had installed "Java Extension Pack", and mentioned "-Dfile.encoding=UTF-8", so, you are debug through "Debugger for Java" extension. that's caused the fails of I want to recurrence your problem.
I tried to edit the encoding of terminal, through task.json, or in the settings.json adds "terminal.integrated.shellArgs.windows": ["/K", "chcp 65001"], but both of them not works.
that's of course, as the "Debugger for Java" extension will open a new terminal to execute the commands, as the settings above will not take a infulence on that terminal.
so, I can recurrence your problem this time, as I open the "launcher.bat" file, which shows in the commands, and change the code -> "chcp.com 65001 > NUL" in it. any other number of chcp.com will caused your problem.
so, maybe, this can provide some inspirations to you.
vscode was different from your original 'javac,java' commands, as vscode depends on extensions to execute those commands.
through "Language Support for Java(TM) by Red Hat" extension compile '.java' file to '.class', it customized add '-encoding utf8' args.
through "Debugger for Java" extension to change the terminal 'chcp' to '65001', and '-Dfile.encoding=UTF-8' to make sure the JVM start with encoding 'utf8'.
you can achieve the same effects through add enviroments variable:
"JDK_JAVAC_OPTIONS":"-encoding utf8",
"JDK_JAVA_OPTIONS":"-Dfile.encoding=UTF-8",
and 'chcp 65001' to temporary change the terminal encoding.
it's very complex and strange, as I can not sure what's the 'java', 'javac' default encoding, I had tried a lot but still confusing, as I googled a lot, but cann't get the enough informations. but everythings are included in those three variables, I hope this can provide you some useful informations, I am too tired. sorry...
for code runner extention,just add this code to user setting json:
"code-runner.executorMap":{"java": "cd $dir && javac -encoding utf-8 $fileName && java $fileNameWithoutExt",},
"code-runner.runInTerminal": true,
I faced the similar issue. My environment is a little bit complicated.
I use both of 'Code-Runner' and 'Java-extenstion Pack.' That seems to make it harder for me to resolve the issue.
After several trying, I found out below things.
When I use run icon (in upper-right side), 'code-runner' compine and run my code in "OUTPUT", which shows broken Korean characters.
When I use "Run > Debugging" or "Run > Run Without Debugging", 'Java extension Pack' compile and run my code in "TERMINAL", which shows Korean characters properly.
Below is my final settings.
A. settings.json in workspace
Insert -encoding utf-8 in 'code-runner.executoMap'
....
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac -encoding utf-8 $fileName && java $fileNameWithoutExt",
.....
"sml": "cd $dir && sml $fileName"
}
B. settings.json in User
Add "java.jdt.ls.vmargs": "-Dfile.encoding=utf-8"
Add "code-runner.runInTerminal": true
After setting, code-runner prints Korean characters well in "TERMINAL"(not "OUTPUT"), and 'java extension pack' also does.

-Xpkginfo:always arguments causes javac 1.7.0_25 to crashes

My goal
We need to pass -Xpkginfo:always arguments to javac compiler. We are using javac 1.7.0_25. -Xpkginfo is available for this version says java doc.
Expected result: package-info.class files shall be generated
Actual result : javac compiler is crashing :
An exception has occurred in the compiler (1.7.0_25). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.NullPointerException
at com.sun.tools.javac.comp.Enter.visitTopLevel(Enter.java:291)
at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:459)
at com.sun.tools.javac.comp.Enter.classEnter(Enter.java:258)
at com.sun.tools.javac.comp.Enter.classEnter(Enter.java:272)
at com.sun.tools.javac.comp.Enter.complete(Enter.java:484)
at com.sun.tools.javac.comp.Enter.main(Enter.java:469)
at com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:929)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)
at com.sun.tools.javac.main.Main.compile(Main.java:439)
at com.sun.tools.javac.main.Main.compile(Main.java:353)
at com.sun.tools.javac.main.Main.compile(Main.java:342)
at com.sun.tools.javac.main.Main.compile(Main.java:333)
at com.sun.tools.javac.Main.compile(Main.java:76)
at com.sun.tools.javac.Main.main(Main.java:61)
What I've tried so far
It works with java 1.8, but unfortunately, we can't upgrade our java compiler because we still have old java code.
EDIT
Used javac -X to make sure 1.7.0_25 has -Xpkginfo argument available. Answer is yes :
PS C:\SVN\products\faa_mx\vs2017.install2> javac -X
-Xlint Enable recommended warnings
-Xlint:{all,cast,classfile,deprecation,dep-ann,divzero,empty,fallthrough,finally,options,overrides,path,processing,rawtypes,serial,static,try,unchecked,varargs,-cast,-classfile,-deprecation,-dep-ann,-divzero,-empty,-fallthrough,-finally,-options,-overrides,-path,-processing,-rawtypes,-serial,-static,-try,-unchecked,-varargs,none} Enable or disable specific warnings
-Xbootclasspath/p:<path> Prepend to the bootstrap class path
-Xbootclasspath/a:<path> Append to the bootstrap class path
-Xbootclasspath:<path> Override location of bootstrap class files
-Djava.ext.dirs=<dirs> Override location of installed extensions
-Djava.endorsed.dirs=<dirs> Override location of endorsed standards path
-Xmaxerrs <number> Set the maximum number of errors to print
-Xmaxwarns <number> Set the maximum number of warnings to print
-Xstdout <filename> Redirect standard output
-Xprint Print out a textual representation of specified types
-XprintRounds Print information about rounds of annotation processing
-XprintProcessorInfo Print information about which annotations a processor is asked to process
-Xprefer:{source,newer} Specify which file to read when both a source file and class file are found for an implicitly compiled class
-Xpkginfo:{always,legacy,nonempty} Specify handling of package-info files
Also tried with 1.7.0_80, same crash :
An exception has occurred in the compiler (1.7.0_80).
java.lang.NullPointerException
at com.sun.tools.javac.comp.Enter.visitTopLevel(Enter.java:291)
at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:459)
at com.sun.tools.javac.comp.Enter.classEnter(Enter.java:258)
at com.sun.tools.javac.comp.Enter.classEnter(Enter.java:272)
at com.sun.tools.javac.comp.Enter.complete(Enter.java:484)
at com.sun.tools.javac.comp.Enter.main(Enter.java:469)
at com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:929)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)
at com.sun.tools.javac.main.Main.compile(Main.java:439)
at com.sun.tools.javac.main.Main.compile(Main.java:353)
at com.sun.tools.javac.main.Main.compile(Main.java:342)
at com.sun.tools.javac.main.Main.compile(Main.java:333)
at com.sun.tools.javac.Main.compile(Main.java:76)
at com.sun.tools.javac.Main.main(Main.java:61)
Alternative solution
Would that make sense to use javac 1.8 and use the command line javac -source 1.7 -target 1.7 MyClass.java?
With java 1.7, it looks like that if you pass -Xpkginfo:always to javac, it only accepts package-info.java empty files. If you pass non-empty files, we get the crash.
With 1.8, javac is able to deal with empty and non empty files in one command line.
So, the workaround with 1.7 is to call javac multiple time, once with all empty package-info.java files, and another time with non-empty files.

How to pass the -encoding parameter to the java compiler from within VS Code?

My Java program's source files are encoded in UTF-8. According to the documentation of the Java compiler:
If -encoding is not specified, the platform default converter is used.
I'm on Windows where the default encoding is cp 850, so I have to add -encoding UTF-8 to javac in order to avoid incorrect accented characters in the console output of my program. The program is displayed fine when I compile it from command-line this way.
However I can't find how to pass this parameter to the Java compiler from within VS Code. I have the Java Extension Pack installed. It provides the launch configuration parameter "args" which passes arguments to the program itself, but there's nothing in the documentation about how to pass parameters to the compiler. I would like this to be done automatically when I press "Run". Please help.
open the settings of code-runner,add the following line:
"code-runner.executorMap": {
"java": "cd $dir && javac -encoding UTF-8 $fileName && java $fileNameWithoutExt"
}

Cygwin terminal error in running .sh file because of a jar file

I am trying to run .jar file for my java code from a .sh shell script file. the jar file name contains "." which is making the Cygwin terminal think it is a directory. Here is the command and the results:
java -jar ./lib/javax.json-1.0.jar
Result:
no main manifest attribute, in lib\javax.json-1.0.jar
Then:
error: package javax.json does not exist
import javax.json.Json;
With this mark ^ below the period (right after javax).
How can I solve it? I am working on Windows 10. Thanks!
EDIT:
I have written many forms of the .sh file to get it run, but it won't run. The current one is:
# !bin/bash
java -jar ./lib/javax.json-1.0.jar
java -jar ./lib/javax.json-api-1.0.jar
javac ./src/TimeTester.java
java TimeTester
Does this look good?
I am getting the following error:
.\src\TimeTester.java:22: error: package javax.json does not exist
import javax.json.Json; (With this ^ below the '.')
AND:
.\src\TimeTester.java:159: error: cannot find symbol
private static JsonObject getJsonFromString(String jsonStr){
And many similar lines in the error.. Any help?
EDIT 2:
This is my current file:
javac -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar ./src/TimeTester.java
java -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar:./src TimeTester
But I am getting:
.\src\TimeTester.java:22: error: package javax.json does not exist
import javax.json.Json;
^
With With this (^) under the last dot (.Json)
EDIT 3:
The current .sh file is:
#!/usr/bin/env bash
cd src
javac -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester.java
java -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester
The first command (javac) works and generates the .class file. BUT, the second command (java) does not work and it gives the following error:
Error: Could not find or load main class TimeTester
Your help is really appreciated!
Final EDIT:
Thanks for Jim, the shell script now works. Now I got a java execution error:
java.io.FileNotFoundException: .\in_input\in.txt (The system cannot find the path specified)
Thanks
TL;DR It is a pain to use Cygwin with programs written for Windows because of the conflicting command-line shell conventions between bash and cmd.exe. To compile and run Java programs it is much better to use an IDE such as Eclipse or Netbeans.
However, if you must...
None of this works because you are trying to pass Linux-style paths to the Windows JVM. However you seem to have a more basic misunderstanding:
# !bin/bash
java -jar ./lib/javax.json-1.0.jar
java -jar ./lib/javax.json-api-1.0.jar
javac ./src/TimeTester.java
java TimeTester
I am surmising that you think the first two statements make the libraries available to the compiler for the third javac line. This is not true, those two lines attempt to execute the jar file, which of course fails since the jar does not contain a main class
What you should be doing is providing those two library paths as arguments to the -cp option of the javac command.
This is where it gets quite tricky, as you are mixing a Linux-style shell emulator with a Windows JVM. Paths that are intended for the shell must remain in Linux style, while paths that are going to be consumed by the JVM must be converted to Windows format, and path strings for the JVM must be delimited with semicolon (Windows style) instead of colon (Linux style). That introduces a further complication since the semicolon in Cygwin (Linux) is the delimiter for multiple commands on one line, so the path string must be quoted to prevent the semicolon from breaking things.
Also problematic is the naming of the class to be compiled. You have not shown us the package declaration of the Java file, but I'm assuming it's in the default package (i.e. there is no package declaration and it's not package src;). In that case you should be in the src directory, not one directory above.
Finally, once you specify -cp, you must also add the current directory to the classpath on Windows if you want it to be included, otherwise it will not find your newly-compiled .class file.
So the compile and execute commands should be
javac -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester.java
java -cp '.;../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester
For simple relative paths the Windows JVM will accept forward slashes, but if you have absolute Linux paths (i.e. /cygdrive/c/..., or with the cygdrive path set to /, paths like /c/user/...) the JVM will not understand them and they will need to be translated using cygpath.
None of your 4 commands work:
java -jar ./lib/javax.json-1.0.jar does not work because javax.json-1.0.jar is not an executable jar file.
java -jar ./lib/javax.json-api-1.0.jar does not work because javax.json-api-1.0.jar is not an executable jar file.
javac ./src/TimeTester.java does not work because your class requires classes from the javax.json package to be on the classpath, and you haven't set the classpath. Classes from the javax.json package are found in the javax.json-1.0.jar file.
java TimeTester does not work because the compilation failed.
To fix all that, remove the first two lines, and specify the classpath on the other two lines, e.g.
javac -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar ./src/TimeTester.java
java -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar:./src TimeTester
Notice that you also had to list ./src on the classpath when executing your program.

I can't run java files on my Notepad++, it shows "CreateProcess() failed with error code 2:"

enter image description here
It seems compile well, but when it run seems not well.
p.s. Eclipse can run well, I mean my JRE install has no problem, so I think the bug has connection with my Notepad++.
As i mentioned earlier, there is a space missing:
javac -encoding UTF-8 "$(FULL_CURRENT_PATH)" java -cp"$(CURRENT_DIRECTORY)" "$(NAME_PART)"
^ here
It should be
javac -encoding UTF-8 "$(FULL_CURRENT_PATH)" java -cp "$(CURRENT_DIRECTORY)" "$(NAME_PART)"

Categories