ZT: JAVA and JAVAC command line; java compile and run with the package name, the package comes with external dependencies jar run

ZT: https://blog.csdn.net/just3do/article/details/68957618

Sometimes write a small test, I do not want to open the idea, to use cmder, but forget how old compiled, a copy someone else's blog.

javac and java -classpath command line option

This is a very basic question, but because basically using existing programs to develop java IDE tools, so few people realize it.
javac
-classpath, set the path to search for class, it can be a directory, jar files, zip files (which are class files) will overwrite all settings in CLASSPATH inside.
-sourcepath, to set the search path needed to compile java files, can be a directory, jar files, zip files (which are the java file).
So a complete javac command line should look like this,
assuming that abc.java in the path c: \ src inside, can execute the following command in any directory to compile.
javac -classpath c: \ classes; c : \ jar \ abc.jar; c: \ zip \ abc.zip -sourcepath c: \ source \ project1 \ src; c: \ source \ project2 \ lib \ src.jar; c : \ source \ project3 \ lib \ src.zip c: \ src \ abc.java

expressed the need to compile c: \ classed following class file, c: \ jar \ abc.jar inside the class file, c: \ zip \ abc inside the .zip file class
also need to c: \ source \ project1 \ src the following source file, c: \ source \ project2 \ lib \ src.jar inside the source file, c: \ source \ project3 \ lib \ src.zip inside the source file,
note: jar, zip inside the source file will not be any changes to the source files in the directory, there are likely to be recompiled.
java
-classpath, set the path to search for class, can be a directory, jar files, zip files (which are class files) will overwrite all CLASSPATH settings.
Since part of the class is to be executed to search for the class, the class must take this path is also provided -classpath placed inside.
Manifested in the implementation of java in the class path to be executed there, we must add a dot (.) Have indicated in this catalog search.

Suppose abc.class path c: \ src which
may be performed in any order the following path
Java -classpath C: \ classes; C: \ JAR \ abc.jar; C: \ ZIP \ abc.zip; c: \ src ABC

question: If main.class belongs to c: \ jar \ abc.jar, and com.cnblogs.jeffchen this package, then the implementation of java -classpath c: \ classes; c : \ jar \ abc.jar; c: \ zip \ abc.zip; com.cnblogs.jeffchen.main can, but if classpath contains multiple jar bag? And other jar package also com.cnblogs.jeffchen what will happen? error?

In windows,
dividing the file path for a backslash \   
class java file or list delimiter semicolon;

in linux
separator bits slash file path /
class java file list or a colon delimiter:

a examples compile and run linux
 /usr/local/java/bin/javac -classpath /tmp/javatest/lib/mail-1.3.3.jar -d /tmp/javatest/bin/ /tmp/javatest/src/jp/co/realseed/Capability.java

/usr/local/java/bin/java -classpath /tmp/javatest/lib/mail-1.3.3.jar:/tmp/javatest/bin/ jp.co.realseed.Capability

-----------------------------------------------------------------

 

Today wrote a test program with the package name, and even forgot how to compile and run, so write it down now

1 compile and run the program with a package

 
  1. package test;  
  2.   
  3. public class  HiveJdbcClient {   
  4.   
  5.     public static void main(String[] args) {  
  6.         System.out.println("-------------------");  
  7.     }  
  8.   
  9. }  

 

The program with a package name, the compiler should use

 
  1. javac -d . HiveJdbcClient.java  

Here to generate a test directory in the current directory will be, which is HiveJdbcClient.class file

When running the command:

[
  1. java test.HiveJdbcClient  

operation result:

 
  1. -------------------  


2 对于需要依赖其他jar的运行

在实际的运行中,可能需要依赖额外的jar包,那么javac 和 Java 应该怎么做呢

使用 java -cp 指定依赖的jar包就可以。例子如下:

HiveJdbcClient.java

[java]  view plain  copy
 
  See the code CODE on the sheetI derived the piece of code
  1. #package test;  
  2.   
  3. import java.sql.Connection;  
  4.   
  5.   
  6. import java.sql.DriverManager;  
  7. import java.sql.ResultSet;  
  8. import java.sql.SQLException;  
  9. import java.sql.Statement;  
  10. // import org.apache.hive.jdbc.HiveDriver;  
  11.   
  12. public class HiveJdbcClient {  
  13.   
  14.     private static String driverName = "org.apache.hive.jdbc.HiveDriver";  
  15.   
  16.     public boolean run() {  
  17.   
  18.         try {  
  19.             Class.forName(driverName);  
  20.             Connection con = null;  
  21.             con = DriverManager.getConnection(  
  22.                     "jdbc:hive2://192.168.17.15:10000/hivedb""hiveuser""hiveuser");  
  23.             Statement stmt = con.createStatement();  
  24.             ResultSet res = null;  
  25.   
  26.             String sql = "select count(*) from test_data";  
  27.   
  28.             System.out.println("Running: " + sql);  
  29.             res = stmt.executeQuery(sql);  
  30.             System.out.println("ok");  
  31.             while (res.next()) {  
  32.                 System.out.println(res.getString(1));  
  33.   
  34.             }  
  35.             return true;  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.             System.out.println("error");  
  39.             return false;  
  40.         }  
  41.   
  42.     }  
  43.   
  44.     public static void main(String[] args) throws SQLException {  
  45.         HiveJdbcClient hiveJdbcClient = new HiveJdbcClient();  
  46.         hiveJdbcClient.run();  
  47.     }  
  48.   
  49. }  

里面的代码里,我们依赖了Hive的jdbc jar 包,在编译和运行时我们也要加上依赖的jar包,需要注意的是,使用 java -cp 有额外的jar的时候:在Linux下面ClassPath前面是一个点号加一个冒号;在Windows下面ClassPath前面是一个点号加一个分号

 
  1. javac -cp .;D:\ochadoop4.0.1\hive-0.13.1-cdh5.2.1-och4.0.1\user_lib\hive--jdbc-0.13.1-cdh5.2.1.jar HiveJdbcClient.java  

运行命令:

 
  1. java -cp .;D:\ochadoop4.0.1\hive-0.13.1-cdh5.2.1-och4.0.1\user_lib\hive-jdbc-0.13.1-cdh5.2.1.jar HiveJdbcClient  

这样就可以了

 

如果我们把代码中的 package 注释打开(该文件又多了一个包)
那么,编译时使用:

 
  1. javac -cp .;D:\ochadoop4.0.1\hive-0.13.1-cdh5.2.1-och4.0.1\user_lib\hive--jdbc-0.13.1-cdh5.2.1.jar -d . HiveJdbcClient.java  

Run the command:

 
  1. java -cp .;D:\ochadoop4.0.1\hive-0.13.1-cdh5.2.1-och4.0.1\user_lib\hive-jdbc-0.13.1-cdh5.2.1.jar test.HiveJdbcClient  

that's it

 

-d (points): it represents the class which hit the file directory

Guess you like

Origin www.cnblogs.com/wtjqs/p/11616174.html