Feeling after reading Coding problem of running Jar parameters in Java console

Source: Internet
Author: User

The following page, too let me tangled, who can help adjust a bit, ...

_________________

Java-jar Test.jar "121"


In this way, "121" if printed out, it will be garbled, you can add a-dfile.encoding=utf-8 to specify the parameters as what character encoding incoming.


Java-dfile.encoding=utf-8-jar Test.jar "121"


In this way, the Java code in the printed out is not garbled.


Other than that:

string represents a UTF-16-formatted string in which the supplementary character is represented by a surrogate pair (for more information, see Unicode in the Character class Character representation ). The index value refers to a char unit of code, so the supplementary character occupies two positions in the String .

The String class provides methods for processing Unicode code points (that is, characters) and Unicode code units (that is, char values).

getBytes : The character set here is a sequence of bytes that encodes a string into a specified character set, and the string in Java must be UTF-8, such as the official document excerpt, which is not known to be correct.

Public byte[] getBytes(Charset Charset)

Encodes this String into a byte sequence using the given charset and stores the result in a new byte array.

This method always uses the default substitution byte array for this character set to replace the wrong input and the non-mapped character sequence. If you need more control over the encoding process, you should use the Charsetencoder class.

Parameters:

CharSet - charset for encoding String

Return:

The resulting byte array

Start from the following versions:

1.6


string: The character set here is a description of what character set the byte array is encoded in, and this character set specifies the parsing method to construct the UTF-8 string string.

Public String(byte[] bytes, Charset Charset)

Constructs a new Stringby decoding the specified byte array with the specified charset . The length of the new String is a function of the character set, and therefore may not be equal to the length of the byte array.

This method always replaces the error input and the non-mapped character sequence with the default substitution string for this character set. If you need more control over the decoding process, you should use the Charsetdecoder class.

Parameters:

bytes -byte to decode as character

CharSet -The charset to be used to decode bytes

Start from the following versions:

1.6



Java-dfile.encoding=utf-8 Kill garbled

FEB 10TH, | COMMENTS

Reference Links:

Java garbled problem-dfile.encoding=utf-8

Java ' s file.encoding property on Windows platform

How does the open a jar file on a Mac?

More

The ins and outs of encountering garbled problems

These two days to write a Java program to play, the results have encountered many times before the garbled problem, specific description:

In the MAC system, the common Java program startup methods are as follows:

1. Launch the class entry file through Eclipse;

2. Launch in Terminal with Java Test.class or Jave-jar Test.jar

3. Boot through the Ant execution Class entry file;

4. Execute the jar file directly with Ant;

5. Execute the jar file with the jar Launcher.app in Mac OS coreservices.

6. Use the Mac OS's own jar Bundler.app to wrap the jar file into an app and then execute

The execution path is still quite rich, but in a different way, the program output from the console is inconsistent.

For example, just in Eclipse Chinese characters that can be printed correctly in the Jar package Later,

Double-click the Jar file to Jar Launcher.app the way to start, print out the text will become garbled.

after all, it's written. Java The program will eventually be hit Jar It's not always in the bag . Eclipse in the start?

As I said earlier, this is not the first time I have encountered such a problem, so I want to solve this problem.

Brainwave think of a good way, in these start-up mode System To print out the property traversal in the

and then use git to do the different versions of the comparison, there may be some clues to set out ~

hold the idea of a try to practice a handful, and indeed found some fishy, concentrated reflected in file.encoding this attribute above.

in the file.encoding the value of the property is UTF-8 , there is no garbled problem, Eclipse This is the case with execution.

Jar Launcher.app when executed, the value of this property becomes Macroman out,

there is an introduction to this attribute in the information given above, which can be used java-d<name>=<value> Test.jar to change it.

In addition, only the startup Java before the program is changed by passing parameters, the program can no longer be changed once it is started.

In this case, the default encoding changes are made only by passing the JVM parameters:

One, write a band -dfile.encoding=utf-8 parameter of the script file to start;

secondly, with Jar Bundler.app packaged into app , the efficiency should be less than the first option.

The principle is almost the same, all just change JVM The default encoded operation is encapsulated and is no longer required for manual typing.

Java garbled problem-dfile.encoding=utf-8

-dfile.encoding Explanation:

Enter Java on the command line, and the-D description appears in the hints given:

-d<name>=<value> # Set a System property

-D needs to be followed by a key-value pair to set a system property

the -dfile.encoding=utf-8 is to set system Properties file.encoding to be UTF-8

so file.encoding What do you mean? literal meaning for file encoding.

Search Java source code, can only find 4 files contained in file.encoding the file,

in other words, only four files are called file.encoding this property.

in the Java.nio.charset in the package Charset.java , the meaning of this passage is very clear.

Simply put, the default character set is determined when the Java Virtual machine is started.

Depends on the region of the operating system where the Java virtual machine resides and the character set.

as you can see in the code, the default character set is file.encoding obtained from this attribute.

Java ' s file.encoding property on Windows platform

The used for the default encoding in Java, all readers and writers would the "File.encoding" is a set to the default locale of the Windows operationg system since Java 1.4.2. System.getproperty ("file.encoding") can is used to access the property. Code such as System.setproperty ("file.encoding", "UTF-8") can is used to the change the property. However, the default encoding can is changed dynamically even this property can be changed. So the conclusion was that the default encoding can ' t was changed after JVM starts. "Java-dfile.encoding=utf-8" Can is used to set the default encoding when starting a JVM. I have searched for this option Java official documentation. But I can ' t find it.

How does the open a jar file on a Mac?

You can indeed launch a jar file from the command line, with the following command:

Java-jar Yourfile.jar

As well as the default app for this can assign "Jar Launcher" as the. Double-click a jar file, as follows (I don t believe you need the developer tools installed for this):

Click once on the. jar file in the Finder and then from the MenuBar in the Finder Select file, Get Info ". Click on ' Open with ' and from the popup menu select ' Other '. A file Browser window would open. In this window, go to the /system/library/coreservices folder and select ' Jar Launcher '. Then make sure the ' Always Open with ' checkbox is checked and then click Add. Then click the ' Change all ' button so, any jar file would be opened automatically. Finally, close the Info window and now if you double-click any of the your jar files they should run automatically.

Posted by Yang3wei Feb 10th, java related




Feeling after reading Coding problem of running Jar parameters in Java console

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.