$ javac Increment.java
Output:
Increment.java:6: ??: ?????
System.out.println(++a++);
^
??: ??
??: ?
1 ???
here is the code
class Increment{
public static void main(String[] args) {
int a = 5;
System.out.println(++a++);
}
}
Does any one know what may be happening and how to fix it?
Increment is just a class for testing so that a error will appear.
I am running it in git-bash terminal, but I have tried it in cygwin terminal and windows's terminal as well.
character-set is UTF-8.
Most probably you have a national locale set up (e.g. Russian, Chinese or anything else) that makes the Java compiler to return nationalized error messages, but your terminal (cygwin) does not support the UTF-8 output or your system does not support UTF-8 locale.
As the quickest work-around you could switch java compiler to provide error messages in English:
$ javac -J-Duser.language=en Increment.java
Related
Well, here is the problem, I have started using VScode, and I can't read from console cyrillic characters.
My code:
import java.util.Scanner;
class App {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in, "UTF-8");
String word = input.nextLine();
System.out.println(word);
}
}
Now when I enter any cyrillic string it will print empty string back to me. If i write something like
System.out.println("Привет"); //cyrillic symbols
It will print "Привет", which is fine. So I am guessing it has something to do with reading the string rather than outputing it.
chcp command gives Active code page: 65001
I have tried setting encoding and without it, but it doesn't seem to work, is there something I missed?
Thanks in advance
I've tested the code on my machine and got the same result: nothing shown;
You can see, when run it in external Window PowerShell or Command Prompt, the result is different but still not shown correctly:
When we change the encode style to GBK(936), the cyrillic characters can be displayed correctly:
When it comes to changing integrated terminal encoding style in vscode and execute code again, it still shows nothing:
About these different results between external Command Prompt and integrated terminal in VS Code, I've put a github request. And I'm doing some research, if any useful imformation i get, i will update you.
I'm using the terminal in Mac to try and output some strings using javac. However there are some symbols that don't seem to work, for instance the dollar sign and asterisk:
public class BirdDisplay{
public static void main(String... args){
System.out.println(args[1]);
}
}
and then:
javac BirdDisplay.java
java BirdDisplay sparrow $someBird
I get this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at BirdDisplay.main(BirdDisplay.java:3)
As far as I know $ is accepted in class names and is a valid identifier, what is the cause of this exception?
You're using it from a shell, where $ is doing environment/shell variable substitution. This has nothing to do with Java - it's how the shell is invoking the process.
Just put it in single quotes:
java BirdDisplay sparrow '$someBird'
Note that the use of a $ as a valid Java identifier is irrelevant, as you're not using it in any source code - the value $someBird purely being used as data in your program (or will be once you've prevented the shell from performing variable substitutions).
As Daisy pointed out, this is because you are running your program in the shell, where $someBird is interpreted as an environment variable. Because $someBird is not an environment variable, the shell replaces it with nothing and you have a command-line arguments array of length 1 instead of length 2. As such, your program has no value for args[1] and you get java.lang.ArrayIndexOutOfBoundsException. You can test this by running this code to print out the length of args:
public class BirdDisplay{
public static void main(String... args){
System.out.println(args.length);
}
}
And now when you do:
javac BirdDisplay.java
java BirdDisplay sparrow $someBird
You will see 1 instead of 2
I met some interesting question on which I was not able to find an answer. Does anybody know how to pass hieroglyphs as an argument to the main method from command line?
Below there is some pseudo code which will help to test suggested solution:
public class Test {
public static void main(String args[]) {
if ("香港政府".equals(args[0])) {
System.out.println("Match");
}
}
}
So question is how to call Test.class with an argument so the application prints Match to the console? args[0] can be transformed before passing to the if statement.
Thanks in advance.
After some additional research I was able kinda figured it out. So guys who commented on question were very near to the answer.
Encoding which I tried to find was 936. But it doesn't mean that you will be able to run chcp 936 if you OS locale is other than chinese. Once you will try to run it on other locale than chinese:
chcp 936
You will get following error:
Invalide code page
For making it working you have to change region. FOllowing steps will be needed:
Start - COntrol panel
Select "Region and Language"
Select "Administrative" and click "Change system locale..."
Select "Chinese (Simplified, PRC)" and reboot laptop
After restart when you will run chcp you will see following output Active code page: 936. Now you are ready to execute command line with hieroglyphs.
I'm trying to parse my command line arguments using the apache commons CLI. It might be a bit heavy handed for the example here, but it makes sense in the context of the program I'm creating. I'm trying to read a file pattern filter, similar to what grep uses to select files to process.
My Argument looks like this:
Program --input *.*
I've written a test program to see what the parser is seeing;
public static void main(String[] args) {
Options options = new Options();
options.addOption(new Option(INPUT_FILTER_SHORT, INPUT_FILTER_LONG, true, INPUT_FILTER_DESCRIPTION));
CommandLineParser parser = new BasicParser();
CommandLine cmd = parser.parse(options, args);
System.out.println(cmd.getOptionValue(INPUT_FILTER_SHORT));
}
This prints out:
.classpath
If I change my arguments to:
Program --input test.txt
I get the output:
test.txt
I'm assuming that I have to do something to tell apache commons what * is not a special character? I can't seem to find anything about this online.
I'm experiencing this on Windows (7). I'm fairly certain it's the *.* which is causing the issue as when I swap to using patterns that don't use *, the expected pattern shows up.
Your problem isn't really to do with Commons CLI, but to do with how the shell and the Java executable together process the parameters.
To eliminate other factors, and see what's going on, use a short Java program:
public class ArgsDemo {
public static void main(String[] args) {
for(int i=0; i<args.length; i++) {
System.out.println("" + i + ": " + args[i]);
}
}
}
Play with java ArgsDemo hello world, java ArgsDemo * etc. and observe what happens.
On UNIX and Linux:
Java does no special processing of *. However, the shell does. So if you did:
$ mkdir x
$ cd x
$ touch a b
$ java -jar myjar.jar MyClass *
... then MyClass.main() would be invoked with the parameter array ["a","b"] -- because the UNIX shell expands * to files in the current directory.
You can suppress this by escaping:
$ java -jar myjar MyClass * // main() sees ["*"])
(Note that a UNIX shell wouldn't expand *.* to .classpath because this form would ignore "hidden" files starting with .)
On Windows
cmd.exe does not do UNIX-style wildcard expansion. If you supply * as a parameter to a command in Windows, the command gets a literal *. So for example, PKUNZIP *.zip passes *.zip to PKUNZIP.EXE, and it's up to that program to expand the wildcard if it wants to.
Since some release of Java 7, the Java executable for Windows does some wildcard to filename expansion of its own, before passing the parameters to your main() class.
I've not been able to find clear documentation of Java-for-Windows' wildcard expansion rules, but you should be able to control it with quoting, escaping the quotes to prevent cmd.exe interpreting them:
> java.exe -jar myjar.jar MyClass """*.*"""
(Untested as I don't have a Windows box handy, and quoting in cmd.exe is a bit of a beast - do please experiment and either edit the above or leave a comment)
I have a java program that takes a bunch of arguments. One of these arguments is meant to be a file glob that I wish to give to the program without interpolation (as is).
For example: s3sync -match '*.xml{,.gz}' ...
This works perfectly fine from the command line. s3sync itself is a minimal shell script that sets up some environment vars and finally does:
$EXEC java $DEBUG $MEMORY -cp "$CLASSPATH" com.my.packages.S3Sync "$#"
However, when I try my code from an Eclipse Debug Configuration, I'm unable to tell Eclipse NOT to interpolate that arg. Here is what I tried, and what comes in as String[] args in main():
in Program Arguments what it becomes in main(String[] args)
-match '*.xml' String[]{"-match", "'*.xml'"}
-match *.xml String[]{"-match", "build.xml", "ivy.xml", ...}
-match "*.xml" String[]{"-match", "build.xml", "ivy.xml", ...}
-match \*.xml String[]{"-match", "\\*.xml"}
Any idea? How do I write an argument in Eclipse Debug Configurations > Program Arguments so that the arg[] is "*.xml"?
This dialog has been problematic for this and similar reasons.
The only character you can escape in that dialog is the double quote character. You could try escaping the double quotes in
-match \"*.xml\"
and see if you can get the "*.xml" passed in correctly, albeit with quotes on it.
Other than that, I think you'll have to support an alternate syntax that you translate into the correct syntax in your code.