{"id":2230,"date":"2024-12-17T17:40:15","date_gmt":"2024-12-17T09:40:15","guid":{"rendered":"https:\/\/thereisno.top\/?p=2230"},"modified":"2024-12-17T17:55:00","modified_gmt":"2024-12-17T09:55:00","slug":"java-%e5%91%bd%e4%bb%a4%e8%a1%8c%e6%89%93%e5%8c%85%e6%88%90%e5%8f%af%e6%89%a7%e8%a1%8cjar","status":"publish","type":"post","link":"https:\/\/thereisno.top\/?p=2230","title":{"rendered":"java \u547d\u4ee4\u884c\u6253\u5305\u6210\u53ef\u6267\u884cjar"},"content":{"rendered":"\n<p>\u4f7f\u7528IDE\u6253\u5305\u662f\u5f88\u5bb9\u6613\u7684\u4e8b\u60c5\uff0c\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0d\u80fd\u4f7f\u7528IDE\uff0c\u53ea\u80fd\u4f7f\u7528\u547d\u4ee4\u884c\u6253\u5305<\/p>\n\n\n\n<p>jar -cvf my.jar -C target <mark style=\"background-color:#fcb900\" class=\"has-inline-color\">.<\/mark><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>-C target . \u53c2\u6570\uff0c\u6307\u5b9a\u628a target \u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u6253\u5305<br>\u6267\u884c\u9700\u8981\u6307\u5b9a\u7a0b\u5e8f\u5165\u53e3,\u5426\u5219\u62a5\u9519no main manifest attribute\uff0c\u56e0\u4e3ajava \u4e0d\u77e5\u9053\u5e94\u8be5\u6267\u884c\u4ec0\u4e48\u3002<br>\u6ce8\u610f\uff1a\u547d\u4ee4\u6700\u540e\u7684\u70b9\uff0c\u8868\u793a\u6240\u6709\u6587\u4ef6\uff0cC\u53c2\u6570\u540e\u7684\u76ee\u5f55\u9700\u8981\u662f\u7f16\u8bd1\u7684\u76ee\u6807\u76ee\u5f55\uff0c\u662fpackage\u7684\u4e0a\u7ea7\u76ee\u5f55\uff0c\u5982\u679c\u4ee3\u7801\u7684package\u662fcom.a.b\uff0c\u5207\u5fcc\u4e0d\u8981\u9009\u62e9com\u6216\u4ee5\u4e0b\u76ee\u5f55\u3002Package\u76ee\u5f55\u548c\u6587\u4ef6\u5939\u8def\u5f84\u4e00\u81f4\uff0c\u5426\u5219\u627e\u4e0d\u5230class\u3002<br>target<br> |&#8212;-com<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&#8212;-a<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&#8212;&#8211;b<br>java -jar my.jar<br>\u6b63\u786e\u7684\u6267\u884c\u65b9\u5f0f<\/p>\n\n\n\n<p>java -cp my.jar com.demo.Hello<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-cp my.jar \u628a jar \u5305\u6dfb\u52a0\u5230 classpath \u76ee\u5f55\ncom.demo.Hello \u8bf4\u660e\u54ea\u4e2a\u7c7b\u662f\u5165\u53e3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6253\u5305\u53ef\u6267\u884c jar<\/h2>\n\n\n\n<p><br>\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\uff0c\u65e0\u9700\u6307\u5b9a\u5165\u53e3<br>\u521b\u5efaMANIFEST.MF\u6587\u4ef6\uff0c\u4f4d\u7f6e\u53ef\u4ee5\u4efb\u610f\u4e0d\u5fc5\u653e\u5728META-INF\u4e0b\uff0c\u6253\u5305\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u521b\u5efaMETA-INF\uff0c\u5728\u6587\u4ef6\u5185\u8bf4\u660e\u7a0b\u5e8f\u5165\u53e3\uff1a<br>Main-Class: com.demo.Hello<br>\u518d\u6b21\u6253\u5305<\/p>\n\n\n\n<p>\u6dfb\u52a0\u4e86 m \u53c2\u6570\uff0c\u6307\u5b9a MANIFEST.MF\uff0c\u8fd9\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u4f1a\u88ab\u6dfb\u52a0\u5230 jar \u5305\u7684\u6e05\u5355\uff0c\u7528\u4e8e\u8bf4\u660e my.jar \u7684\u5165\u53e3<\/p>\n\n\n\n<p>jar -cvfm my.jar MANIFEST.MF -C target .<br>\u4f9d\u8d56\u7b2c\u4e09\u65b9 jar \u5305\u7684 java \u4ee3\u7801\u7684\u7f16\u8bd1\u6267\u884c<br>\u521b\u5efa\u6587\u4ef6 src\/com\/demo\/HelloGson.java\uff0c\u8fd9\u4e2a\u4ee3\u7801\u4f9d\u8d56 gson \u5305\uff1a<br>package com.demo;<br>import com.google.gson.Gson;<br>import java.util.HashMap;<br>import java.util.Map;<br>public class HelloGson {<br>public static void main(String[] args) {<br>Gson gson = new Gson();<br>Map map = new HashMap();<br>map.put(&#8220;key1&#8221;, &#8220;value1&#8221;);<br>map.put(&#8220;key2&#8221;, &#8220;value2&#8221;);<br>String json = gson.toJson(map);<br>System.out.println(json);<br>}<br>}<br>\u76f4\u63a5\u7f16\u8bd1\uff1a<br>javac src\/com\/demo\/HelloGson.java -d target<br>\u6b64\u65f6\u4f1a\u62a5\u9519 error: package com.google.gson does not exist\uff0c\u56e0\u4e3a\u4f9d\u8d56\u5305\u4e0d\u5b58\u5728<br>\u521b\u5efa libs \u76ee\u5f55\uff0c\u628a\u4f9d\u8d56\u5305 gson-2.8.5.jar \u653e\u8fdb\u53bb\uff0c\u6700\u540e\u6574\u4e2a\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"494\" height=\"465\" src=\"https:\/\/thereisno.top\/wp-content\/uploads\/2024\/12\/image-5.png\" alt=\"\" class=\"wp-image-2245\" srcset=\"https:\/\/thereisno.top\/wp-content\/uploads\/2024\/12\/image-5.png 494w, https:\/\/thereisno.top\/wp-content\/uploads\/2024\/12\/image-5-300x282.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<p>\u6dfb\u52a0classpath\u53c2\u6570\u518d\u6b21\u7f16\u8bd1\uff0c\u4f7f\u7528 -cp libs\/gson-2.8.5.jar \u53c2\u6570\u6dfb\u52a0\u4f9d\u8d56\u5305\uff1a<br>javac -cp libs\/gson-2.8.5.jar src\/com\/demo\/HelloGson.java -d target<\/p>\n\n\n\n<p>\u6267\u884c\u65f6\u4e5f\u9700\u628a libs\/gson-2.8.5.jar \u6dfb\u52a0\u5230 classpath \u8def\u5f84\u4e2d\uff1a<br>java -cp target:libs\/gson-2.8.5.jar com.demo.HelloGson<\/p>\n\n\n\n<p>\u5426\u5219\u4f1a\u62a5\u9519\u7f3a\u5c11\u4f9d\u8d56 java.lang.ClassNotFoundException<br>\u624b\u52a8\u6dfb\u52a0Class-Path\u6b63\u786e\u65b9\u6cd5<br>Manifest-Version: 1.0<br>Class-Path: . testPackage2_lib\/fastjson-1.2.38.jar<br>Main-Class: com.mym.practice.testpackage.MainClass<\/p>\n\n\n\n<p>\u6ce8\u610f<br>\u2022 \uff081\uff09\u6709\u591a\u4e2ajar\u5f15\u7528\u8981\u6362\u884c\u5199<br>\u2022 \uff082\uff09\u6ce8\u610fClass-Path\u7684\u5192\u53f7\u4e0e\u53f3\u8fb9\u5b57\u7b26\u6709\u7a7a\u683c\uff0c\u5e76\u4e14\u6709\u4e2a\u70b9\u201c.\u201d\u4e0d\u80fd\u6f0f\u4e86\uff0c\u770b\u5f88\u591a\u6587\u7ae0\u90fd\u6ca1\u6709\u8bf4\u5230\u8fd9\u4e2a\u70b9\uff0c\u5bfc\u81f4\u5373\u4f7f\u52a0\u4e86\u8fd9\u4e2a\u4e5f\u6ca1\u7528\u3002\u8fd9\u4e2a\u70b9\u7684\u7531\u6765\u53ef\u4ee5\u53c2\u8003\u914d\u7f6ejdk\u73af\u5883\u53d8\u91cf\u914d\u7f6eclasspath\u65f6\u662f\u4e00\u6837\u7684<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><br>\u5982\u679c\u672a\u628ajar\u5305\u6253\u5305\u8fdb\u53bb\uff0c\u5982\u4f55\u907f\u514d\u8fd0\u884c\u627e\u4e0d\u5230\u4f9d\u8d56\u5305<\/h2>\n\n\n\n<p><br>1\u3001\u4e0d\u63a8\u8350\uff1a\u628a\u4f9d\u8d56\u7684jar\u5168\u90e8\u653e\u5230JRE\u7684\u6269\u5c55\u7c7b\u6587\u4ef6\u5939\u4e2d\uff08%JAVA_HOME%\/jre\/lib\/ext\uff09<br>2\u3001\u4f7f\u7528 -Djava.ext.dirs=..\/lib<\/p>\n\n\n\n<p>\u76f8\u5173\u6587\u7ae0<\/p>\n\n\n\n<p><a href=\"https:\/\/thereisno.top\/?p=502\">Java \u547d\u4ee4\u884c\u7f16\u8bd1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528IDE\u6253\u5305\u662f\u5f88\u5bb9\u6613\u7684\u4e8b\u60c5\uff0c\u4f46\u662f\u67d0\u4e9b\u60c5\u51b5\u4e0d\u80fd\u4f7f\u7528IDE\uff0c\u53ea\u80fd\u4f7f\u7528\u547d\u4ee4\u884c\u6253\u5305 jar -cvf my.jar  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/thereisno.top\/?p=2230\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cjava \u547d\u4ee4\u884c\u6253\u5305\u6210\u53ef\u6267\u884cjar\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[243,8,244],"class_list":["post-2230","post","type-post","status-publish","format-standard","hentry","category-java","tag-jar","tag-java","tag-244"],"_links":{"self":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2230"}],"version-history":[{"count":11,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2230\/revisions"}],"predecessor-version":[{"id":2246,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2230\/revisions\/2246"}],"wp:attachment":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}