Java programmer's daily work-experience stickers (pure dry goods), java programmers do goods

Source: Internet
Author: User

Java programmer's daily work-experience stickers (pure dry goods), java programmers do goods

The tasks encountered at work are complex, so there are many knowledge points involved. Now, let's record the knowledge points we met today. It's just dry ~

File decompression and compression. If your system does not support the tar-z command

If it is an old Unix system, you may not know the tar-Z command. If you want to compress the tar.gz file, you need to use the gzip, gunzip, and tar commands.

According to tar.gz, the compressed package at the end of tar is only responsible for packaging the file and not compressing it. The compressed package at the end of gz is used for compression.

For this reason, the tar.gz file can be understood as, first packaging, and then compression.

Then, the compressed command can be written as follows:

tar -cvf abc.tar abcgzip -c abc.tar > abc.tar.gz

A file named abc.tar.gz will eventually be generated. Similarly, if you want to decompress the package, you can:

Gunzip abc.tar.gz => This command first obtains the abc.tar file tar-xvf abc.tar => to decompress the command.

After these two commands are executed, an abc folder will appear in the current folder.

If your system supports the tar-z command

If your system level is higher, you don't have to worry about it. The tar command can directly perform operations on gz:

Tar-zxvf compressed file name .tar.gz => This command can directly decompress the compressed file tar-zcvf compressed file name .tar.gz compressed file name => This command can directly compress tar.gz
File handle occupation causes application crash

In Java, ifExcessive stream operationsOrEnable too many unclosed socketsAnd may not be closed in time.too many open files. This is because the number of file handles in the system is insufficient ....

In linux, you can run the following command to view the number of file handles:

ulimit -n

You can also use this command to modify it:

ulimit -n 2048

However, here is a temporary solution. If the file handle is not released for a long time, an error will still be reported.

Therefore, you should return to the program and check the stream operation:

BufferedReader in = null; try {in = new BufferedReader (new FileReader (file); // your business logic} catch (Exception e) {} finally {if (in! = Null) {try {in. close (); // release in time} catch (Exception e ){}}}

If it is a reusable stream, you can extract it for multiple times.

Garbled characters in Linux

Garbled code is often plagued by the daily development of programmers, and coding issues are not detailed. One common problem is that a well-developed application will encounter garbled characters in Linux. carefully check the configuration of each encoding, Which is UTF-8, which is just a bit confusing.

In fact, this is a JVM problem, because the JVM will execute according to the system encoding by default. If the JVM encoding is incorrect, the internal file processing will of course be garbled.

First, check the default encoding of the system:

# localeLANG=LC_CTYPE="C"LC_COLLATE="C"LC_MONETARY="C"LC_NUMERIC="C"LC_TIME="C"LC_MESSAGES="C"LC_ALL=

This is the encoding of many systems.CIn this blog, C is the default Locale of the system, which is supported by ansi c by default. That is to say, the default encoding is ansi c!

In this way, it and our UTF-8 is definitely not consistent. Therefore, you can:

java -Dfile.encoding=UTF-8 xxxx

Add the preceding parameters to specify the JVM encoding. If you are started in tomcat, you can modify the java-related parameters. If you are using other programs, modify the corresponding startup Command Based on the jvm parameters at startup.

Use javac and java to execute class

This is a basic knowledge, but General developers may just use it to experiment with helloworld. For example:

Javac HelloWorld. java => compile HelloWorld. classjava HelloWorld => execute this class

The actual situation may be far more complex than this:

How to start the jar package compiled in eclipse

Using Eclipse for packaging is relatively simple:

  • Right-click the project name-Export
  • Select Jar File
  • Select the specified project and the directory of the compiled jar package
  • Click finish to package

At this time, if you directly execute java-jar xxx. jar, an exception may be thrown:

Java-jar target. jarfileMonitor. jar does not have the master list attribute.

This is because the Main method definition is missing in this jar. At this point you can do this, through the decompression tool into the jar package, modify the MENIFEST. MF file under the META-INF.

Manifest-Version: 1.0Main-Class: com. test. Class Name

Note that there must be spaces after the colon following Main-Class and the last line should be blank (if there is no carriage return on the last line, an error will be reported that the attribute Main-Class cannot be found ).

If you use Javac and java to compile classes

If you have a class, this class depends on other jar packages, for example, test. java depends on a. jar and B. jar.
You can execute javac for compilation:

Javac-cp a. jar; B. jar test. java => Note: For Linux, replace the semicolon with the colon javac-cp a. jar: B. jar test. java.

Then run the following command in java:

Java-cp.; a. jar; B. jar test => for linux, replace the semicolon with the colon java-cp.: a. jar: B. jar test
Write shell scripts

Some people often write scripts similar to one-click tomcat startup. Here we use linux as an example:

#! /Bin/shPRG = "$0" PRGDIR = 'dirname "$ PRG" '[-z "$ ROOT_PATH"] & ROOT_PATH = 'CD "$ PRGDIR /.. ">/dev/null; pwd 'echo "set ROOT_PATH to $ ROOT_PATH" [-z "$ JRE_HOME"] & JRE_HOME = 'CD "$ ROOT_PATH/jre">/dev/null; pwd 'echo "sets JRE_HOME to $ JRE_HOME" "$ JRE_HOME"/bin/java-Dfile. encoding = UTF-8-jar "$ AGENT_PATH"/lib/test. jar

There are several areas worth learning:

  • The first point is how to set environment variables, such as using the built-in jre

    PRG = "$0" PRGDIR = 'dirname "$ PRG" 'is used to obtain the directory where the startup script is located. [-Z "$ ROOT_PATH"] & ROOT_PATH = 'CD "$ PRGDIR /.. ">/dev/null; pwd' specifies the root directory of the application where the startup script is located [-z "$ JRE_HOME"] & JRE_HOME = 'CD "$ ROOT_PATH/lib/jre"> /dev/null; pwd' is the final command for setting environment variables. Vulgar JRE_HOME is specified as the built-in jre of the application.
  • Second, how to start our own class

    "$JRE_HOME"/bin/java -Dfile.encoding=UTF-8 -jar "$AGENT_PATH"/lib/test.jar

    The above command is to execute the built-in java command in the jre, use the java command to start an executable jar package, and set its encoding.

Notes for learning Java !!!

If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group: 299541275 let's learn Java together!

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.