(Some summary of beginners ~ Master do not spray ha ~)
Reason:
Java has been running in the ECLISPE programming environment before. Very comfortable, like using shoot camera photography generally.
Have seen many masters are directly using Vim to edit files, command-line compilation run, think that is not more cumbersome?
The turning point was after the first few days of the graduation topic selected. Bishi problem is a dynamic analysis of class relationship based on Java bytecode. You need to make changes to the bytecode in the. class file (specifically, after a number of instructions are added to mark the new instructions to achieve the purpose of dynamic tracking).
I found that eclipse was simply not so flexible that he could not directly load and run a. class file that I modified. It is the general case of taking care of most. It does a lot of things for us: automatically compiles. java source files into a. class byte file to load the class and run. But it is not enough to meet my personal needs. The command line, though troublesome, is more essential.
At least from this point of view, the Java command-line compilation run is still very important.
I consulted a lot of online information, found that although more information, but not complete, is not too clear. So tidy up as follows, hope for the beginning of Java command line compilation run the package some help it!
Many beginners compile run time of the exception, the following methods can be solved ~ if you encounter any problems, take a closer look at the following first ~ may be helpful oh.
The basic concept of Java's operating mechanism:
The source file is our familiar. java file.
class file is a compiler compiled from a. java file. As we all know, Java is a cross-platform of the Java Virtual Machine (JVM) This layer of hardware isolation, and. class files can be understood as executable files in the JVM (their own understanding, may be less accurate). It stores Java bytecode, Java bytecode is stack-based (stack based) (more detailed official explanations of bytecode and JVM can be referred to the Java Virtual Machine specification , If the book is too thick, another programming for the Java Virtualmachine is recommended.
compiling Java compilation generally refers to the conversion process from a source file (. java file) to a class file (. class file). In the JDK command line is the JAVAC command (the abbreviation of Java compiler ~ with the C-language Wood)
Run the. class file in the JVM. is a Java command.
The CLASSPATH environment variable , which stores the directory to be searched when compiling a file or running a class. For example, if you have a class in Hello.java that uses a third-party package Thirdpart.jar, you must add the appropriate path to the classpath so that the compiler can find it. (note ~ Put Thirdparty.jar in the current working directory, do not tell the environment variables are not OK ~ compiler only recognize environment variables!) Similarly, when you run a class of files, there are third-party jar packages that must also be added to Classpath. In general, there are three ways to modify environment variables.
1. In Javac or Java commands, use the-classpath option followed by the desired directory address. Obviously, this method can only take effect within the scope of the current statement.
2. Direct command line to modify Classpath or PATH # path= $JAVA _home/bin:/home/username/bin
#export PATH
However, this method can only be effective in this operation.
3. The permanent method is to modify the configuration file. In/etc/profile or/ETC/PROFILE.D or other, different operating system distributions are located differently. Add classpath= to the file ... (corresponding path), restart the computer (you can also execute the source command, so that no restart is already in effect ~# SOURCE/ETC/PROFILE.D actually. The same effect as source Oh ~ #. /ETC/PROFILE.D)
The PATH environment variable is similar to classpath, except that it is not used to look for classes, but rather to find Java-related executable files. You can use the Java-version command to see if you have already set the path (if the detailed Java information is displayed, you have set it up, if not, you need to find the installation location of Java, reset)
For specific information about the environment variables, refer to Doc path and CLASSPATH and Setting the class path.
Jar Package
Java uses the concept of the package to avoid duplicate naming problems. A bit like the C + + namespace. Classes in the same package can be used directly. Different packages, you need to import the corresponding package into the header of the. java file. Beginners write the HelloWorld program of course is not to notice the problem of the package, but when the project is getting bigger, the likelihood of duplicate naming increases, we have to rely on the concept of packaging to better manage our code.
At the same time, in order to facilitate management, transmission, the jar package appears.
The jar package is actually a zip-compressed file package. We can pack our own package, easy to reuse, where sure enough, you can also refer directly to the folder (note that you must refer to the root directory of the folder, such as the file is defined as the package Mypackage.foo, That must put myapackage this folder along with the internal Foo file on the corresponding path)
The command to make the jar package and decompress the package is as follows:
jar -cvf foo.jar foo
The last parameter is a package that needs to be compressed. -CVF a few options, F must be placed at the end, followed by the F must be the output file name. V indicates output details (verbose)
The corresponding decompression commands are:
jar -xvf foo.jar
Note that there is an optional mainifest file in the meta-inf/manifest. MF path.
We can add the following statement to the MANIFEST.MF file
Main-class:mypackage.myclass
Specifies that the MyClass class is a main class with a main () entry. Using the following statement, you can execute the corresponding program.
java -jar foo.jar
Command
Javac command: Compiling source files
-classpath the options that beginners must master, the third-party class used in the following class (in the form of a jar or zip or a file package). Under Linux, multiple directories are separated by colons:
It is important to note that the content in the-classpath will overwrite the contents of the environment variable CLASSPATH.
-CP is-classpath's abbreviation.
-D Create the directory where the generated. class file is stored.
-o This option tells Javac to optimize the code generated by the inline static, final, and Privite member functions.
-verbose This option tells Java to display information about the compiled source file and any called class libraries. For example,-verbose:class can see various classes of loading information. -VERBOSE:GC is the information of garbage collection.
There are many option usually use not much (say I also actually just will use, not skilled directly in the command line environment Debug compile =), need to use the time directly to flip the man Javac good.
java Command: Load run class file
-classpath and Javac in the-classpath same truth.
-CP is the abbreviation of-classpath.
-jar executing a program on the main class defined on the jar package
An example of compiling and running a. java file is as follows:
# Javac-classpath./:/home/username/bin/thirdparty.jar Hello.java
#java-classpath./:/home/username/bin/thirdparty.jar Hello
In addition, for Java files with package information, do the following:
$ ls.# CurrentDirectory contains the"X" Packagex$ ls x# The "X" PackageContains aSample.Java file...Sample.java$ Cat X/Sample.Java# ...which looks likeThis.PackageX;Public Class Sample {Public Static voidMain(String...Args) {System.Out.println("Hello from Sample class");}}$ javac X/Sample.Java# Use "/"As Delimiter and#Include the-suffix when Compiling.. Sample# Use as delimiter when Running, and Don t include # the ". Class" Suffix.hello from sample class
Add:The path to eclipse
Under Eclipse, each project can control the path.
1. In the Package Explorer directory, right-click your project. Drop-down menu, click on the bottom of the properties, the left side of the popup window, there is a Java Build path this tab.
This is primarily the introduction of paths to other project packages, third-party jar packages, and the setting of source file paths in project.
2. In the drop-down menu of run (which is the green Run Start button), select Run Configuration.
In each running program, there are several tabs, Main, argument, JRE, classpath, source, environment, common.
Where argument can set the Java command Line runtime parameters. Also try the args in main (string[] args).
Classpath You can set the lookup directory for the system ClassLoader load class. (For class loading, you can refer to another article ClassLoader three principles and Java class loader analysis)
Mined area:
When running under Linux, add a directory, and never mistake the delimiter. Under Windows is \, while Linux is under/.
When you run the class, you do not need to add. class. For example, there is a class Hello.class, Run command is # java Hello instead of #java Hello.class. The operating mechanism is to look for a class, not to find a file as it was compiled.
If the referenced class has a package hierarchy, the referenced path is the starting point of the packet hierarchy and cannot be extended to a directory hierarchy in the package. For example, referring to the third-party class Com.thirdparty.hello in the ~/workspace directory, the Java command-classpath input is the root of the third-party class's package location: #java-classpath ~/ Workspacehello, and cannot be Java-classpath ~/workspace/com/thirdparty Hello.
nosuchmethodexception!
This problem is encountered while running your own program. Find a lot of information, and finally found that the original is a path problem! In the original path, an older version of the. class file is in the preferred position, so each time you invoke a new method, the error = =.
If there is a problem, we can communicate more AH ~
Java Command-line compilation project