Everyday of Java Programmers-experience stickers (pure dry goods)

Source: Internet
Author: User
Tags gz file locale

There are a lot of things involved in the work, so there are many points of knowledge. Here for the record, today's knowledge points, pure dry Goods ~

About extracting and compressing files if your system does not support the TAR-Z command

If you are an old UNIX system, you may not know the tar-z command, so if you want to compress or decompress tar.gz files, you will need to use gzip or gunzip and tar commands.

With regard to tar.gz, it is understood that the tarball at the end of the tar is actually only responsible for packaging the files and not compressing them, while the package at the end of GZ is a compression operation.

Therefore, the tar.gz file can be understood as, first packaged, then compressed.

Then, the compressed command can be written like this:

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

Eventually, a abc.tar.gz file is drawn. Similarly, if you want to decompress, you can:

gunzip abc.tar.gz=>该命令会首先得出一个abc.tar的文件tar -xvf abc.tar=>该命令完成解压的步骤

After executing these two commands, an ABC folder will appear in the current folder.

If your system supports the TAR-Z command

If your system level is a bit higher, you don't have to bother, the tar command can directly manipulate GZ:

tar -zxvf 压缩文件名.tar.gz=>这个命令可以直接完成对压缩文件的解压tar -zcvf 压缩文件名.tar.gz 被压缩文件名=>这个命令可以直接完成对tar.gz的压缩
Application crashes due to file handle occupancy

In Java, if it is 执行过多的流操作 either 开启过多未关闭的Socket , and does not close in a timely manner, errors can occur too many open files . This is because the number of file handles in the system is not enough ....

In Linux, you can use commands to view the number of file handles:

ulimit -n

You can also use this command to make changes:

ulimit -n 2048

But modify here, is a temporary solution, if you do not release the file handle for a long time, will still error.

So you should go back to the program and check the flow action:

in = null;try{    in = new BufferedReader(new FileReader(file)); //你的业务逻辑}catch(Exception e){}finally{ if(in != null){ try{ in.close();//及时的进行释放 }catch(Exception e){ } }}

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

The problem of garbled characters under Linux system

Garbled problem often puzzles the programmer's Daily development, about the coding problem is not detailed. There is a frequently encountered problem is that we develop a good application, put on Linux will appear garbled, carefully check each encoding configuration, are utf-8, is simply baffled its solution.

In fact, this is a problem with the JVM, because the JVM will default to the system code to execute, if the JVM is not encoded, the internal processing of the file will of course be garbled.

First look at the system's default encoding:

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

Many of the system's code is this C , in this blog said, C is the system default locale, by default, ANSI C to support. That means the default encoding is ANSI C!

In this way, it must be inconsistent with our UTF-8. Therefore, you can do this:

java -Dfile.encoding=UTF-8 xxxx

Specify the encoding used by the JVM by adding the above parameters. If you are starting in Tomcat, you can modify the Java-related parameters in it, and if it is another program, modify the corresponding startup command according to the JVM parameters at startup.

Execute class using Javac and Java

This is a basic knowledge, but the general developer may just use it to experiment with HelloWorld. Like what:

javac HelloWorld.java=>编译出HelloWorld.classjava HelloWorld=>执行该类

The actual situation may be far more complicated than this:

How to start a jar package that was compiled in eclipse

Packaging with Eclipse is simple:

    • Right-click Project name-export
    • Select Jar File
    • Select the specified project, and the directory where the jar package is compiled
    • Click Finish to Package

This time, if you execute Java-jar xxx.jar directly, an exception may be thrown:

java -jar target.jarfileMonitor.jar中没有主清单属性

This is because the definition of the main method is missing from this jar. At this point you can do this by extracting the tool into the jar package and modifying the MENIFEST.MF file under Meta-inf.

Manifest-Version: 1.0Main-Class: com.test.类名

Note that the colon after the main-class is preceded by a space, and the last line is empty (if there is no last line of carriage returns, it will be reported that the error Main-class this property is not found).

If you use Javac and Java-compiled classes

If you have a class, this class relies on other jar packages, such as: Test.java relies on A.jar, B.jar.
You can then execute javac to compile:

javac -cp a.jar;b.jar test.java=>注意如果是Linux,分号要换成冒号javac -cp a.jar:b.jar test.java

Then use Java to execute:

java -cp .;a.jar;b.jar test=>如果是linux,分号换成冒号java -cp .:a.jar:b.jar test
Writing shell scripts

There are often people who write scripts like the one-click launcher of Tomcat, which takes Linux for example:

#!/bin/shprg="$ "prgdir= ' dirname"$PRG "' [-Z"$ROOT _path "] && root_path= 'CD "$PRGDIR/.." >/dev/null; pwd 'echo ' setting Root_path to $ROOT _path "[-Z "$JRE _home "] && jre_home= 'cd "$ Root_path/jre ">/dev/null; pwd 'echo ' set Jre_home to $JRE _home ""$JRE _home "/bin/java-dfile.encoding=utf-8-jar  "$AGENT _path"/lib/test.jar             

There are a few places to learn:

  • 1th, how to set environment variables, such as using the built-in JRE

     prg= "$0" prgdir= ' dirname  " $PRG" ' These two words are to get the directory where the startup script is located. [-Z  " $ROOT _path"] && root_path= ' cd  " $ROOT _path/lib/jre ">/dev/null; pwd "is the final command to set environment variables. Vulgar Jre_home is specified as the application's built-in JRE.  
  • 2nd, is how to start our own class

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

    The above command, which executes the Java command in the built-in JRE, launches an executable jar package with the Java command, and sets its encoding.

Learn Java students pay attention to!!!

You are welcome to join the Java Learning Exchange Group when you encounter any problems in the learning process or want to acquire learning resources: 299541275 We'll learn java! together.

Everyday of Java Programmers-experience stickers (pure dry goods)

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.