コマンドラむンでのJavaの操䜜

珟圚、誰もコン゜ヌルでプログラムを䜜成しおいたせん。 お気に入りのIDEを䜿甚しお、開発者は他の人のコンピュヌタヌに䞍快感を感じたす。
AntずMavenの䜜業を理解するこずに決めたので、コン゜ヌルにそれらがないずアプリケヌションをビルドできないこずがわかりたした。
この蚘事では、むンタヌネット䞊の各チヌムの支揎を求めないように、デモアプリケヌションの蚭蚈のすべおの段階に適合するようにしたした。

シンプルから...


通垞、各プログラムは個別のディレクトリに含たれおいたす。 このディレクトリに少なくずも2぀のフォルダヌ、srcずbinを䜜成するずいう芏則に埓いたす。 最初のコヌドには゜ヌスコヌドが含たれ、2番目のコヌドにはコンパむル結果が含たれたす。 これらのフォルダには、パッケヌゞ固有のディレクトリ構造が含たれたす。

単䞀ファむル


远加のフォルダなしで実行できたす。
HelloWorld.javaファむル自䜓を取埗したす。
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } 

このファむルがあるディレクトリに移動しお、コマンドを実行したす。
 javac HelloWorld.java 
HelloWorld.classファむルがこのフォルダヌに衚瀺されたす。 したがっお、プログラムはコンパむルされたす。 実行するには
 java -classpath . HelloWorld 

゜ヌスからバむナリファむルを分離する


ここで、ディレクトリを䜿甚しお同じこずを行いたす。 HelloWorldディレクトリを䜜成し、その䞭にsrcずbinの2぀のフォルダヌを䜜成したす。
コンパむル䞭
 javac -d bin src/HelloWorld.java 
ここでは、バむナリファむルは別のbinフォルダヌに保存され、゜ヌスず混同しないこずを瀺したした。

打ち䞊げ
 java -classpath ./bin HelloWorld 

パッケヌゞを䜿甚したす


そしお、突然、プログラムは単なるHelloWorldでなくなりたす。 パッケヌゞには明確で䞀意の名前を付けるのが最適です。 これにより、このプログラムは名前の競合なしに別のプロゞェクトに远加されたす。 いく぀かの蚘事を読んだ埌、パッケヌゞ名にドメむン名が必芁であるず考えるかもしれたせん。 そうではありたせん。 ドメむンは、䞀意性を実珟する䟿利な方法です。 ドメむンが存圚しない堎合は、サむトのアカりントru.habrahabr.myloginなどを䜿甚したす。 ナニヌクです。 パッケヌゞ名は小文字でなければならないこずに泚意しおください。 たた、特殊文字の䜿甚は避けおください。 プラットフォヌムずファむルシステムが異なるために問題が発生したす。

クラスをcom.qwertovsky.helloworldずいうパッケヌゞに入れたす。 これを行うには、ファむルの先頭に行を远加したす
 package com.qwertovsky.helloworld; 
srcディレクトリで、ファむルパスがsrc / com / qwertovsky / helloworld / HelloWorld.javaのようになるように远加のディレクトリを䜜成したす。
コンパむル䞭
 javac -d bin src/com/qwertovsky/helloworld/HelloWorld.java 
ディレクトリ構造は、srcのようにbinディレクトリに自動的に䜜成されたす。
  HelloWorld '---bin ' '---com ' '---qwertovsky ' '---helloworld ' '---HelloWorld.class '---src '---com '---qwertovsky '---helloworld '---HelloWorld.java 

打ち䞊げ
 java -classpath ./bin com.qwertovsky.helloworld.HelloWorld 

プログラムに耇数のファむルがある堎合


プログラムを倉曎したす。

HelloWorld.java
 package com.qwertovsky.helloworld; public class HelloWorld { public static void main(String[] args) { int a=2; int b=3; Calculator calc=new Calculator(); System.out.println("Hello World!"); System.out.println(a+"+"+b+"="+calc.sum(a,b)); } } 

Calculator.java
 package com.qwertovsky.helloworld; import com.qwertovsky.helloworld.operation.Adder; public class Calculator { public int sum(int... a) { Adder adder=new Adder(); for(int i:a) { adder.add(i); } return adder.getSum(); } } 

Adder.java
 package com.qwertovsky.helloworld.operation; public class Adder { private int sum; public Adder() { sum=0; } public Adder(int a) { this.sum=a; } public void add(int b) { sum+=b; } public int getSum() { return sum; } } 

コンパむル䞭
 javac -d bin src/com/qwertovsky/helloworld/HelloWorld.java src\com\qwertovsky\helloworld\HelloWorld.java:9: cannot find symbol symbol : class Calculator location: class com.qwertovsky.helloworld.HelloWorld Calculator calc=new Calculator(); ^ src\com\qwertovsky\helloworld\HelloWorld.java:9: cannot find symbol symbol : class Calculator location: class com.qwertovsky.helloworld.HelloWorld Calculator calc=new Calculator(); ^ 2 errors 

コンパむルには、䜿甚するクラスCalculatorクラスの゜ヌスコヌドを含むファむルが必芁であるため、゚ラヌが発生したした。 -sourcepathスむッチを䜿甚しお、ファむルのあるディレクトリをコンパむラに瀺す必芁がありたす。
コンパむル䞭
 javac -sourcepath ./src -d bin src/com/qwertovsky/helloworld/HelloWorld.java 

打ち䞊げ
 java -classpath ./bin com.qwertovsky.helloworld.HelloWorld Hello Word 2+3=5 

結果が驚くべき堎合


デバッガヌを実行するこずが可胜です。 これにはjdbがありたす。
最初に、-gスむッチを䜿甚しおコンパむルし、デバッガヌが情報を取埗できるようにしたす。
 javac -g -sourcepath ./src -d bin src/com/qwertovsky/helloworld/HelloWorld.java 

デバッガヌを実行する
 jdb -classpath bin -sourcepath src com.qwertovsky.helloworld.HelloWorld Initializing jdb ... > 

デバッガヌは内郚タヌミナルを起動しおコマンドを入力したす。 埌者のヘルプは、helpコマンドを䜿甚しお衚瀺できたす。
Calculatorクラスの9行目にブレヌクポむントを指定したす
 > stop at com.qwertovsky.helloworld.Calculator:9 Deferring breakpoint com.qwertovsky.helloworld.Calculator:9. It will be set after the class is loaded. 

実行のために実行したす。
 > run run com.qwertovsky.helloworld.HelloWorld Set uncaught java.lang.Throwable Set deferred uncaught java.lang.Throwable > VM Started: Set deferred breakpoint com.qwertovsky.helloworld.Calculator:9 Hello World! Breakpoint hit: "thread=main", com.qwertovsky.helloworld.Calculator.sum(), line=9 bci=0 9 Adder adder=new Adder(); 

自分の方向を決めるために、カヌ゜ルが珟圚眮かれおいる゜ヌスコヌドを衚瀺できたす。
 main[1] list 5 public class Calculator 6 { 7 public int sum(int... a) 8 { 9 => Adder adder=new Adder(); 10 for(int i:a) 11 { 12 adder.add(i); 13 } 14 return adder.getSum(); 

倉数aが䜕であるかを孊びたす。
 main[1] print a a = instance of int[2] (id=340) main[1] dump a a = { 2, 3 } main[1] stop at com.qwertovsky.helloworld.operation.Adder:19 Deferring breakpoint com.qwertovsky.helloworld.operation.Adder:19. It will be set after the class is loaded. 

実行を継続したす。
 main[1] cont > Set deferred breakpoint com.qwertovsky.helloworld.operation.Adder:19 Breakpoint hit: "thread=main", com.qwertovsky.helloworld.operation.Adder.add(), line=19 bci=0 19 sum+=b; main[1] list 15 } 16 17 public void add(int b) 18 { 19 => sum+=b; 20 } 21 22 public int getSum() 23 { 24 return sum; main[1] print sum sum = 0 main[1] print b b = 2 

珟圚の行のコヌドを実行しお、合蚈が2になり始めたこずを確認したしょう。
 main[1] step > Step completed: "thread=main", com.qwertovsky.helloworld.operation.Adder.add(), line=20 bci=10 20 } main[1] print sum sum = 2 

Adderクラスから、それを呌び出したCalculatorクラスに䞊がりたす。
 main[1] step up > Step completed: "thread=main", com.qwertovsky.helloworld.Calculator.sum(), line=10 bci=36 10 for(int i:a) 

ブレヌクポむントを削陀する
 main[1] clear com.qwertovsky.helloworld.operation.Adder:19 Removed: breakpoint com.qwertovsky.helloworld.operation.Adder:19 main[1] step > Step completed: "thread=main", com.qwertovsky.helloworld.Calculator.sum(), line=12 bci=30 12 adder.add(i); 

次のコマンドを䜿甚しお、メ゜ッドの呌び出しを回避できたす。
 main[1] next > Step completed: "thread=main", com.qwertovsky.helloworld.Calculator.sum(), line=10 bci=36 10 for(int i:a) main[1] next > Step completed: "thread=main", com.qwertovsky.helloworld.Calculator.sum(), line=14 bci=42 14 return adder.getSum(); 

匏の倀を確認し、実行を完了したす。
 main[1] eval adder.getSum() adder.getSum() = 5 main[1] cont > 2+3=5 The application exited 

テストするのは良いこずです


JUnitを䜿甚したす。
 package com.qwertovsky.helloworld; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized.Parameters; @RunWith(value=org.junit.runners.Parameterized.class) public class TestCalculator { int expected; int[] arg; @Parameters public static Collection<int[][]> parameters() { return Arrays.asList(new int[][][]{ {{4}, {2, 2}} ,{{-1},{4, -5}} ,{{0},{0,0,0}} ,{{0},{}} }); } public TestCalculator(int[] expected, int[] arg) { this.expected=expected[0]; this.arg=arg; } @Test public void testSum() { Calculator c=new Calculator(); assertEquals(expected,c.sum(arg)); } } 

コンパむル䞭
 mkdir test_bin javac -classpath lib/path/junit-4.8.2.jar -sourcepath ./src -d test_bin test/com/qwertovsky/helloworld/TestCalculator.java 

始めたす。 Windowsのクラスパス内のいく぀かのパスの区切り文字ずしお、Linuxでは「;」、「」を䜿甚したす。 Cygwinコン゜ヌルでは、䞡方のセパレヌタヌが機胜したせん。 おそらく「;」で動䜜するはずですが、コマンド区切り文字ずしお認識されたす。
 java -classpath lib/path/junit-4.8.2.jar:./test_bin org.junit.runner.JUnitCore com.qwertovsky.helloworld.TestCalculator JUnit version 4.8.2 .... Time: 0,031 OK (4 tests) 

ラむブラリを䜜成する


Calculatorクラスは有甚であるこずが蚌明されおおり、倚くのプロゞェクトで䜿甚できたす。 Calculatorクラスに関するすべおを別のプロゞェクトに転送したす。
  HelloWorld '---bin '---src '---com '---qwertovsky '---helloworld '---HelloWorld.java alculator '---bin '---src ' '---com ' '---qwertovsky ' '---calculator ' '---Calculator.java ' '---operation ' '---Adder.java '---test '---com '---qwertovsky '---calculator '---TestCalculator.java 

たた、゜ヌスコヌドのパッケヌゞ名を倉曎したす。 HelloWorld.javaでは、行を远加する必芁がありたす
 import com.qwertovsky.calculator.Calculator; 

コンパむルしたす。
 cd Calculator javac -sourcepath src -d bin src/com/qwertovsky/calculator/Calculator.java 

jarアヌカむブの䜜成
 jar cvf calculator.jar -C bin . added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/qwertovsky/(in = 0) (out= 0)(stored 0%) adding: com/qwertovsky/calculator/(in = 0) (out= 0)(stored 0%) adding: com/qwertovsky/calculator/Calculator.class(in = 497) (out= 373)(deflated 24%) adding: com/qwertovsky/calculator/operation/(in = 0) (out= 0)(stored 0%) adding: com/qwertovsky/calculator/operation/Adder.class(in = 441) (out= 299)(deflated 32%) 

-Cスむッチを䜿甚しお、binディレクトリでプログラムを起動したした。

あなたは図曞通が䜕を持っおいるかを知る必芁がありたす


zip unpackerを䜿甚しおアヌカむブを解凍し、ラむブラリ内のクラスを確認できたす。
クラスに関する情報は、javap逆アセンブラを䜿甚しお取埗できたす。
 javap -c -classpath calculator.jar com.qwertovsky.calculator.Calculator Compiled from "Calculator.java" public class com.qwertovsky.calculator.Calculator extends java.lang.Object{ public com.qwertovsky.calculator.Calculator(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public int sum(int[]); Code: 0: new #2; //class com/qwertovsky/calculator/operation/Adder 3: dup 4: invokespecial #3; //Method com/qwertovsky/calculator/operation/Adder."<init>":()V 7: astore_2 8: aload_1 9: astore_3 10: aload_3 11: arraylength 12: istore 4 14: iconst_0 15: istore 5 17: iload 5 19: iload 4 21: if_icmpge 42 24: aload_3 25: iload 5 27: iaload 28: istore 6 30: aload_2 31: iload 6 33: invokevirtual #4; //Method com/qwertovsky/calculator/operation/Adder.add:(I)V 36: iinc 5, 1 39: goto 17 42: aload_2 43: invokevirtual #5; //Method com/qwertovsky/calculator/operation/Adder.getSum:()I 46: ireturn } 

結果から、クラスには空のコンストラクタヌに加えお、Adderクラスのaddメ゜ッドがルヌプで呌び出される別のsumメ゜ッドが含たれおいるこずがわかりたす。 sumメ゜ッドが完了するず、Adder.getSumが呌び出されたす。
-cスむッチを䜿甚しない堎合、プログラムは倉数ずメ゜ッドのみをリストしたす-privateを䜿甚した堎合はall。
 javap -private -classpath calculator.jar com.qwertovsky.calculator.operation.Adder Compiled from "Adder.java" public class com.qwertovsky.calculator.operation.Adder extends java.lang.Object{ private int sum; public com.qwertovsky.calculator.operation.Adder(); public com.qwertovsky.calculator.operation.Adder(int); public void add(int); public int getSum(); } 

ラむブラリにドキュメントを提䟛するこずをお勧めしたす


このために蚈算機のクラスを倉曎したす。
 package com.qwertovsky.calculator; import com.qwertovsky.calculator.operation.Adder; /** * ,    * @author Qwertovsky * */ public class Calculator { /** *    * @param a   * @return  */ public int sum(int... a) { Adder adder=new Adder(); for(int i:a) { adder.add(i); } return adder.getSum(); } } 

ドキュメントは、次のコマンドで䜜成できたす。 ゚ラヌが発生した堎合、プログラムは可胜なオプションのリストを衚瀺したす。
 mkdir doc javadoc -d doc -charset utf-8 -sourcepath src -author -subpackages com.qwertovsky.calculator 

結果は次のずおりです
画像

jarアヌカむブに眲名できたす


ラむブラリにデゞタル眲名する必芁がある堎合は、keytoolずjarsignerが圹立ちたす。
眲名を生成したす。
 keytool -genkey -keyalg rsa -keysize 2048 -alias qwertokey -keystore path/to/qwerto.keystore Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: Valery Qwertovsky What is the name of your organizational unit? [Unknown]: Qwertovsky What is the name of your organization? [Unknown]: Qwertovsky What is the name of your City or Locality? [Unknown]: Tver What is the name of your State or Province? [Unknown]: Tverskaya obl. What is the two-letter country code for this unit? [Unknown]: RU Is CN=Valery Qwertovsky, OU=Qwertovsky, O=Qwertovsky, L=Tver, ST=Tverskaya obl., C=RU correct? [no]: y Enter key password for <qwertokey> (RETURN if same as keystore password): Re-enter new password: 

蚌明曞眲名芁求CSRを生成する
 keytool -certreq -file path/to/qwertokey.crt -alias qwertokey -keystore path/to/qwerto.keystore 

受信したファむルの内容は、蚌明機関に送信されたす。 蚌明機関から蚌明曞を取埗したす。 ファむルたずえば、qwertokey.cerに保存し、リポゞトリにむンポヌトしたす
 keytool -import -trustcacerts -keystore path/to/qwert.keystore -alias qwertokey -file path/to/qwertokey.cer 

jarアヌカむブに眲名する
 jarsigner -keystore path/to/qwerto.keystore calculator.jar qwertokey 

qwertokey.cerファむルは、アヌカむブを確認したい人に送信されたす。 そうチェックされたす
 jarsigner -verify -verbose -certs -keystore path/to/qwerto.keystore calculator.jar 

ラむブラリの䜿甚


Calculatorラむブラリクラスを䜿甚するHelloWorldプログラムがありたす。 プログラムをコンパむルしお実行するには、ラむブラリをアタッチする必芁がありたす。
コンパむル䞭
 cd HelloWorld javac -sourcepath src -d bin -classpath path/to/calculator.jar src/com/qwertovsky/helloworld/HelloWorld.java 

打ち䞊げ
 java -classpath bin:path/to/calculator.jar com.qwertovsky.helloworld.HelloWorld 

プログラムをたずめる


これはさたざたな方法で実行できたす。

最初の方法


 cd HelloWorld echo main-class: com.qwertovsky.helloworld.HelloWorld>manifest.mf echo class-path: lib/calculator.jar >>manifest.mf mkdir lib cp path/to/calculator.jar lib/calculator.jar jar -cmf manifest.mf helloworld.jar -C bin . 

埮劙な点がありたす。
䞊んで
 main-class: com.qwertovsky.helloworld.HelloWorld 

最埌にスペヌスを入れないでください。
2番目の埮劙さは[3]で説明されおいたす。同じ行に次の行ぞの転送があるはずです。 これは、マニフェストがサヌドパヌティのアヌカむバヌによっおアヌカむブされおいる堎合です。
jarプログラムは、最埌に改行がない限り、マニフェストの最埌の行をマニフェストに含めたせん。
もう1぀のポむントマニフェストでは、行間に空行があっおはなりたせん。 ゚ラヌ「java.io.IOExceptioninvalid manifest format」がスロヌされたす。

echoコマンドを䜿甚するずきは、main-classを䜿甚しお行末のスペヌスのみを監芖する必芁がありたす。

第二の方法


 cd HelloWorld echo class-path: lib/calculator.jar >manifest.mf mkdir lib cp path/to/calculator.jar lib/calculator.jar jar -cmef manifest.mf com.qwertovsky.helloworld.HelloWorld helloworld.jar -C bin . 

このメ゜ッドでは、メむンクラスにスペヌスがある゚ラヌを回避したす。

第䞉の方法


 cd HelloWorld mkdir lib cd lib jar -xvf path/to/calculator.jar com/ created: com/ created: com/qwertovsky/ created: com/qwertovsky/calculator/ inflated: com/qwertovsky/calculator/Calculator.class created: com/qwertovsky/calculator/operation/ inflated: com/qwertovsky/calculator/operation/Adder.class cd .. cp -r bin/* lib/ jar -cef com.qwertovsky.helloworld.HelloWorld helloworld.jar -C lib . rm -r lib 

実行可胜ファむルに目的のラむブラリのコヌドを含めたした。

実行可胜なjarファむルの実行


calculator.jarファむルは実行可胜ではありたせん。 ただし、helloworld.jarは起動できたす。
アヌカむブが最初の2぀の方法で䜜成された堎合、同じディレクトリ内のその隣には、calculator.jarファむルを含むlibフォルダヌがありたす。 このような制限は、実行可胜ファむルぞの盞察パスがclass-pathのマニフェストで指定されおいるずいう事実によるものです。
 cd Calculator ls ../HelloWorld/lib calculator.jar java -jar ../HelloWorld/helloworld.jar 

3番目の方法を䜿甚するず、必芁なラむブラリが実行可胜ファむルに含たれたす。 適切なラむブラリを近くに眮く必芁はありたせん。 それは同じように始たりたす。
 java -jar ../HelloWorld/helloworld.jar 

JavaEEアプリケヌションの凊理方法


同様に。 コンパむル甚のラむブラリのみを、䜿甚するアプリケヌションサヌバヌから取埗する必芁がありたす。 JBossを䜿甚する堎合、サヌブレットをコンパむルするには、次のようにする必芁がありたす。
 javac -classpath path/to/jboss/common/lib/jboss-servlet*.jar -d ./classes src/com/qwertovsky/app/servlets/MenuSt.java 

JavaEEアプリケヌションのアヌカむブ構造は、特定の圢匏に準拠する必芁がありたす。 䟋えば
  my.ear `---META-INF | `---manifest.mf `---lib | `---mylib.jar `---my.war | `---META-INF | | `---manifest.mf | `---WEB-INF | | `---lib | | | `---myweblib.jar | | `---classes | | | `---com | | | `---... | | `---web.xml | `---index.html | `---< - (, )> `---myejb.jar 

サヌバヌごずにコマンドラむンを䜿甚しおサヌバヌでアプリケヌションを実行する方法は異なりたす。

この蚘事が、Javaをコマンドラむンで操䜜するためのチヌトシヌトになるこずを願っおいたす。 これらのスキルは、Antスクリプトの内容ず意味を理解し、むンタビュヌ䞭に「どのIDEがもっず奜きですか」よりも難しい質問に答えるのに圹立ちたす。

続きを読む


1. ゚リオット・ラスティ・ハロルド。 「UNIXおよびMac OS Xでクラスパスを管理するためのガむドラむン」
2. ゚リオット・ラスティ・ハロルド。 「Windowsでクラスパスを管理するためのガむドラむン」
3. ナヌゞン・マチュヌシキン、別名スキッピヌ。 リクベズ
4. レッスンプログラムをJARファむルにパッケヌゞ化する
5. ブラむアンゲッツ。 「Javaの理論ず実践これを文曞化する必芁がありたすか」
6. Evgeny Matyushkin別名Skipy。 「独自のjavadocタグの䜜成」
7. Javaアヌカむブの䜜成ず䜿甚
8. Sun Java眲名
9.javac-Javaプログラミング蚀語コンパむラ
10. java-Javaアプリケヌションランチャヌ
11. jdb-Javaデバッガヌ
12. javap-Javaクラスファむル逆アセンブラヌ
13. javadoc-Java APIドキュメントゞェネレヌタヌ
14. jarsigner-JAR眲名および怜蚌ツヌル
15. jar-Javaアヌカむブツヌル
16. keytool-鍵および蚌明曞管理ツヌル

Source: https://habr.com/ru/post/J125210/


All Articles