Jigsaw 椤圭洰锛欽ava 妯″潡绯荤粺鏂版墜寮曞
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jigsaw 椤圭洰锛欽ava 妯″潡绯荤粺鏂版墜寮曞相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/pre' title='pre'>pre
瑕嗙洊 浠g爜鐗囨 port 鎿嶄綔绯荤粺 ref -- sea creat 鍓嶈█
闅忕潃 2017 骞?10 鏈?Java 9 鐨勫彂甯冿紝Java 鑳藉浣跨敤妯″潡绯荤粺浜嗭紝浣嗘槸涓枃浜掕仈缃戜笂鐨勮祫鏂欏お灏戯紝璁稿鍏充簬 Java 妯″潡绯荤粺鐨勬枃绔犻兘鍙槸浠嬬粛浜嗘ā鍧楃郴缁熺殑濂藉锛屾垨鑰呯粰浜嗕竴浜涙鏃犵粍缁囩殑浠g爜鐗囨锛屾柊鎵嬪湪绗竴娆′娇鐢ㄦā鍧楃郴缁熸椂寰€寰€涓嶇煡閬撳浣曚笅鎵嬨€?/p>
濂藉湪 OpenJDK 瀹樻柟鏂囨。缁欏嚭浜嗕竴涓緢璇︾粏鐨勬柊鎵嬪紩瀵硷紝鍗充娇鏄粠娌′娇鐢ㄨ繃妯″潡绯荤粺鐨勪汉锛屾寜鐓ф柊鎵嬪紩瀵间篃鑳藉畬鎴愯嚜宸辩殑绗竴涓?Java 妯″潡銆傛垜鍦ㄨ繖閲屽彧鏄皢鍏剁炕璇戞垚涓枃锛堣嫳璇按骞虫湁闄愶紝濡傛湁绾版紡锛屾杩庢寚鍑猴級锛屽笇鏈涙洿澶氫汉鑳藉浼氬浣曚娇鐢ㄦā鍧楃郴缁燂紝鍔犻€?Java 绫诲簱鐨勬ā鍧楀寲杩涚▼銆?/p>
鍘熸枃鍦板潃锛?a class=" wrap external" href="https://link.zhihu.com/?target=http%3A//openjdk.java.net/projects/jigsaw/quick-start" rel="nofollow noreferrer" target="_blank" data-za-detail-view-id="1043">Project Jigsaw: Module System Quick-Start Guide
Jigsaw 椤圭洰锛氭ā鍧楃郴缁熸柊鎵嬪紩瀵?/h2>
杩欑瘒鏂囨。缁欏紑濮嬩娇鐢ㄦā鍧楃郴缁熺殑寮€鍙戣€呮彁渚涗簡涓€涓畝鍗曠ず渚嬨€?/p>
绀轰緥涓殑鏂囦欢璺緞浣跨敤鍓嶆枩鏉?/)锛岃矾寰勫垎闅旂鏄啋鍙?:)銆備娇鐢ㄥ井杞殑 Windows 鎿嶄綔绯荤粺鐨勫紑鍙戣€呭湪璺緞涓簲褰撲娇鐢ㄥ悗鏂滄潬()锛岃矾寰勫垎闅旂涓哄垎鍙?;)銆?/p>
Greetings
绗竴涓ず渚嬫槸涓€涓悕鍙?code>com.greetings
鐨勬ā鍧楋紝瀹冨彧鏄畝鍗曠殑鎵撳嵃涓€鍙モ€淕reetings鈥濄€傝繖涓ā鍧楃敱涓や釜婧愭枃浠剁粍鎴愶細妯″潡澹版槑鏂囦欢(module-info.java)鍜屼富绫汇€?/p>
涓轰簡鏂逛究锛屾ā鍧楃殑婧愮爜鏀惧湪涓€涓拰妯″潡鍚嶇浉鍚岀殑鐩綍涓€?/p>
src/com.greetings/com/greetings/Main.java
src/com.greetings/module-info.java
$ cat src/com.greetings/module-info.java
$ cat src/com.greetings/com/greetings/Main.java
package com.greetings;
public class Main {
public static void main(String[] args) {
System.out.println("Greetings!");
}
}
婧愮爜琚笅闈㈢殑鍛戒护缂栬瘧鍒扮洰鏍囩洰褰?code>mods/com.greetings
涓幓锛?/p>
$ mkdir -p mods/com.greetings
$ javac -d mods/com.greetings src/com.greetings/module-info.java src/com.greetings/com/greetings/Main.java
鐜板湪鎴戜滑鐢ㄤ笅闈㈢殑鍛戒护鏉ヨ繍琛岀ず渚嬶細
$ java --module-path mods -m com.greetings/com.greetings.Main
--module-path
鏄ā鍧楄矾寰勶紝瀹冪殑鍊兼槸涓€涓垨澶氫釜鍖呭惈妯″潡鐨勭洰褰曘€?code>-m
閫夐」鎸囧畾涓绘ā鍧楋紝鍒嗛殧绗﹀悗闈㈢殑鍊兼槸涓绘ā鍧椾腑鍖呭惈 main 鏂规硶鐨勭被鐨勭被鍚嶃€?/p>
Greetings world
绗簩涓ず渚嬫洿鏂颁簡org.astro
妯″潡鐨勬ā鍧楀0鏄庢枃浠舵潵澹版槑渚濊禆銆傛ā鍧?code>org.astro
瀵煎嚭浜?API 鍖?code>org.astro
銆?/p>
src/org.astro/module-info.java
src/org.astro/org/astro/World.java
src/com.greetings/com/greetings/Main.java
src/com.greetings/module-info.java
$ cat src/org.astro/module-info.java
module org.astro {
exports org.astro;
}
$ cat src/org.astro/org/astro/World.java
package org.astro;
public class World {
public static String name() {
return "world";
}
}
$ cat src/com.greetings/module-info.java
module com.greetings {
requires org.astro;
}
$ cat src/com.greetings/com/greetings/Main.java
package com.greetings;
import org.astro.World;
public class Main {
public static void main(String[] args) {
System.out.format("Greetings %s!%n", World.name());
}
}
妯″潡琚緷娆$紪璇戙€傜紪璇?code>com.greetings
妯″潡鐨?code>javac
鍛戒护鎸囧畾浜嗕竴涓ā鍧楄矾寰勶紝鎵€浠ュ妯″潡org.astro
鐨勫紩鐢ㄣ€佷互鍙婂畠鎵€瀵煎嚭鐨勫寘涓殑绫诲瀷閮藉彲浠ヨ鎵惧埌銆?/p>
$ mkdir -p mods/org.astro mods/com.greetings
$ javac -d mods/org.astro src/org.astro/module-info.java src/org.astro/org/astro/World.java
$ javac --module-path mods -d mods/com.greetings src/com.greetings/module-info.java src/com.greetings/com/greetings/Main.java
杩欎釜绀轰緥鐨勮繍琛屾柟寮忓拰绗竴涓緥瀛愬畬鍏ㄤ竴鏍凤細
$ java --module-path mods -m com.greetings/com.greetings.Main
Greetings world!
澶氭ā鍧楃紪璇?/h2>
鍦ㄥ墠闈㈢殑绀轰緥涓紝妯″潡com.greetings
鍜屾ā鍧?code>org.astro
鏄垎鍒紪璇戠殑銆傚叾瀹炲湪涓€涓?code>javac
鍛戒护涓紪璇戝涓ā鍧椾篃鏄彲浠ョ殑锛?/p>
$ mkdir mods
$ javac -d mods --module-source-path src $(find src -name "*.java")
$ find mods -type f
mods/com.greetings/com/greetings/Main.class
mods/com.greetings/module-info.class
mods/org.astro/module-info.class
mods/org.astro/org/astro/World.class
鎵撳寘
鐩墠涓烘锛岀ず渚嬩腑琚紪璇戠殑妯″潡鏁h惤鍦ㄦ枃浠剁郴缁熶腑銆備负浜嗘洿鏂逛究鐨勪紶杈撲笌閮ㄧ讲锛岄€氬父浼氬皢妯″潡鎵撳寘鎴愪竴涓?em>modular JAR
锛堟ā鍧楀寲鐨?JAR 鍖咃級銆備竴涓?modular JAR 鐩稿綋浜庝竴涓寘鍚簡涓€涓?module-info.class 鍦ㄥ畠鐨勯《灞傜洰褰曠殑鏅€?JAR 鍖呫€備笅闈㈢殑渚嬪瓙鍦ㄧ洰褰?mlib 涓垱寤轰簡涓€涓?code>[email protected]
鍜?code>com.greetings.jar
銆?/p>
$ mkdir mlib
$ jar --create --file=mlib/[email protected] --module-version=1.0 -C mods/org.astro .
$ jar --create --file=mlib/com.greetings.jar --main-class=com.greetings.Main -C mods/com.greetings .
$ ls mlib
com.greetings.jar [email protected]
鍦ㄨ繖涓緥瀛愪腑锛屾ā鍧?code>org.astro
琚墦鍖呮椂鏍囪瘑浜嗗畠鐨勭増鏈彿鏄?1.0 銆傛ā鍧?code>com.greetings
琚墦鍖呮椂鏍囪瘑浜嗗畠鐨勪富绫绘槸com.greetings.Main
銆傛垜浠幇鍦ㄥ彲浠ヤ笉闇€瑕佹寚瀹氫富绫绘潵鎵ц妯″潡com.greetings
浜嗭細
$ java -p mlib -m com.greetings
Greetings world!
閫氳繃浣跨敤-p
鏉ヤ唬鏇?code>--module-path
锛屽懡浠ゅ彲浠ヨ缂╃煭銆?/p>
jar 宸ュ叿鏈夎澶氭柊鐨勯€夐」锛堥€氳繃jar -help
鏌ョ湅锛夛紝鍏朵腑涓€涓氨鏄墦鍗颁竴涓?modular JAR 鐨勬ā鍧楀0鏄庛€?/p>
$ jar --describe-module --file=mlib/[email protected]
[email protected] jar:file:///d/mlib/[email protected]/!module-info.class
exports org.astro
requires java.base mandated
缂哄皯瀵煎叆鎴栫己灏戝鍑?/h2>
鐜板湪鎴戜滑鏉ョ湅鐪嬪浜庡墠闈㈢殑渚嬪瓙锛屽鏋滃湪妯″潡com.greetings
鐨勬ā鍧楀0鏄庝腑锛屾垜浠笉灏忓績婕忓啓浜嗗紩鐢ㄩ」(requires)灏嗕細鍙戠敓浠€涔堛€?/p>
$ cat src/com.greetings/module-info.java
module com.greetings {
// requires org.astro;
}
$ javac --module-path mods -d mods/com.greetings src/com.greetings/module-info.java src/com.greetings/com/greetings/Main.java
src/com.greetings/com/greetings/Main.java:2: error: package org.astro is not visible
import org.astro.World;
^
(package org.astro is declared in module org.astro, but module com.greetings does not read it)
1 error
鎴戜滑鐜板湪淇浜嗚繖涓ā鍧楀0鏄庯紝浣嗘槸鍗村紩鍏ヤ簡鍙︿竴涓敊璇紝杩欐鎴戜滑婕忓啓浜嗘ā鍧?code>org.astro
鐨勬ā鍧楀0鏄庝腑鐨勫鍑洪」(exports)锛?/p>
$ cat src/com.greetings/module-info.java
module com.greetings {
requires org.astro;
}
$ cat src/org.astro/module-info.java
module org.astro {
// exports org.astro;
}
$ javac --module-path mods -d mods/com.greetings src/com.greetings/module-info.java src/com.greetings/com/greetings/Main.java
$ javac --module-path mods -d mods/com.greetings src/com.greetings/module-info.java src/com.greetings/com/greetings/Main.java
src/com.greetings/com/greetings/Main.java:2: error: package org.astro is not visible
import org.astro.World;
^
(package org.astro is declared in module org.astro, which does not export it)
1 error
鏈嶅姟
鏈嶅姟鑳藉璁╂湇鍔℃秷璐硅€呮ā鍧楀拰鏈嶅姟鎻愪緵鑰呮ā鍧楄В鑰︺€?/p>
杩欎釜渚嬪瓙鏈変竴涓湇鍔℃秷璐硅€呮ā鍧楀拰涓€涓湇鍔℃彁渚涜€呮ā鍧楋細
- 妯″潡com.socket
瀵煎嚭浜嗙綉缁滃鎺ュ瓧鐨?API 銆傝繖涓?API 鍦ㄥ寘com.socket
涓墍浠ヨ繖涓寘琚鍑恒€傝繖涓?API 鏄?em>鍙嫈鎻?/em>鐨勶紝鍏佽涓嶅悓鐨勫疄鐜般€傛湇鍔$被鍨嬫槸鐩稿悓妯″潡涓殑com.socket.spi.NetworkSocketProvider
绫诲瀷锛屽洜姝ゅ寘com.socket.spi
涔熻瀵煎嚭銆?br>- 妯″潡org.fastsocket
鏄竴涓湇鍔℃彁渚涜€呮ā鍧椼€傚畠鎻愪緵浜嗕竴涓com.socket.spi.NetworkSocketProvider
鐨勫疄鐜般€傚畠涓嶅瀵煎嚭浠讳綍鍖呫€?/p>
涓嬮潰鐨勬槸妯″潡com.socket
鐨勬簮鐮侊細
$ cat src/com.socket/module-info.java
module com.socket {
exports com.socket;
exports com.socket.spi;
uses com.socket.spi.NetworkSocketProvider;
}
$ cat src/com.socket/com/socket/NetworkSocket.java
package com.socket;
import java.io.Closeable;
import java.util.Iterator;
import java.util.ServiceLoader;
import com.socket.spi.NetworkSocketProvider;
public abstract class NetworkSocket implements Closeable {
protected NetworkSocket() { }
public static NetworkSocket open() {
ServiceLoader<NetworkSocketProvider> sl
= ServiceLoader.load(NetworkSocketProvider.class);
Iterator<NetworkSocketProvider> iter = sl.iterator();
if (!iter.hasNext())
throw new RuntimeException("No service providers found!");
NetworkSocketProvider provider = iter.next();
return provider.openNetworkSocket();
}
}
$ cat src/com.socket/com/socket/spi/NetworkSocketProvider.java
package com.socket.spi;
import com.socket.NetworkSocket;
public abstract class NetworkSocketProvider {
protected NetworkSocketProvider() { }
public abstract NetworkSocket openNetworkSocket();
}
涓嬮潰鐨勬槸妯″潡org.fastsocket
鐨勬簮鐮侊細
$ cat src/org.fastsocket/module-info.java
module org.fastsocket {
requires com.socket;
provides com.socket.spi.NetworkSocketProvider
with org.fastsocket.FastNetworkSocketProvider;
}
$ cat src/org.fastsocket/org/fastsocket/FastNetworkSocketProvider.java
package org.fastsocket;
import com.socket.NetworkSocket;
import com.socket.spi.NetworkSocketProvider;
public class FastNetworkSocketProvider extends NetworkSocketProvider {
public FastNetworkSocketProvider() { }
@Override
public NetworkSocket openNetworkSocket() {
return new FastNetworkSocket();
}
}
$ cat src/org.fastsocket/org/fastsocket/FastNetworkSocket.java
package org.fastsocket;
import com.socket.NetworkSocket;
class FastNetworkSocket extends NetworkSocket {
FastNetworkSocket() { }
public void close() { }
}
涓轰簡绠€娲侊紝鎴戜滑鍚屾椂缂栬瘧涓や釜妯″潡銆傚湪瀹炶返涓湇鍔℃秷璐硅€呮ā鍧楀拰鏈嶅姟鎻愪緵鑰呮ā鍧楀嚑涔庢€绘槸鍒嗗埆缂栬瘧鐨勩€?/p>
$ mkdir mods
$ javac -d mods --module-source-path src $(find src -name "*.java")
鏈€鍚庢垜浠慨鏀规垜浠殑com.greetings
妯″潡鏉ヤ娇鐢?API 銆?/p>
$ cat src/com.greetings/module-info.java
module com.greetings {
requires com.socket;
}
$ cat src/com.greetings/com/greetings/Main.java
package com.greetings;
import com.socket.NetworkSocket;
public class Main {
public static void main(String[] args) {
NetworkSocket s = NetworkSocket.open();
System.out.println(s.getClass());
}
}
$ javac -d mods/com.greetings/ -p mods $(find src/com.greetings/ -name "*.java")
鏈€鍚庢垜浠潵杩愯瀹冿細
$ java -p mods -m com.greetings/com.greetings.Main
class org.fastsocket.FastNetworkSocket
杈撳嚭缁撴灉璇佹槑鏈嶅姟鎻愪緵鑰呮槸瀛樺湪鐨勶紝骞朵笖瀹冭NetworkSocket
浣滀负宸ュ巶浣跨敤銆?/p>
閾炬帴
jlink 鏄竴涓摼鎺ュ伐鍏凤紝鍙互娌跨潃渚濊禆閾炬潵閾炬帴涓€缁勬ā鍧楋紝鍒涘缓涓€涓敤鎴锋ā鍧楄繍琛屾椂闀滃儚锛堣 JEP 220锛夈€?/p>
璇ュ伐鍏风洰鍓嶈姹傛ā鍧楄矾寰勪腑鐨勬ā鍧楅兘鏄鐢?modular JAR 鎴栬€?JMOD 鏍煎紡鎵撳寘鐨勩€侸DK 鏋勫缓鐢?JMOD 鏍煎紡鎵撳寘鏍囧噯鐨勩€佸拰 JDK 鎸囧畾鐨勬ā鍧椼€?/p>
涓嬮潰鐨勪緥瀛愬垱寤轰簡涓€涓寘鍚?code>com.greetings
妯″潡浠ュ強鍏朵紶閫掍緷璧栫殑杩愯鏃堕暅鍍忥細
jlink --module-path $JAVA_HOME/jmods:mlib --add-modules com.greetings --output greetingsapp
--module-path
鐨勫€兼槸鍖呭惈浜嗚鎵撳寘鐨勬ā鍧楃殑 璺緞銆傚湪寰蒋鐨?Windows 鎿嶄綔绯荤粺涓灏嗚矾寰勫垎闅旂 鈥?鈥?鏇挎崲涓?鈥?鈥?銆?/p>
$JAVA_HOME/jmods
鏄寘鍚簡java.base.jmod
鍜屽叾浠栨爣鍑?JDK 妯″潡鐨勭洰褰曘€?/p>
妯″潡璺緞涓殑mlib
鐩綍鍖呭惈浜嗘ā鍧?code>com.greetings
鐨勯儴浠?artifact)銆?/p>
jlink 宸ュ叿鏀寔璁稿楂樼骇鐨勯€夐」鏉ヨ嚜瀹氫箟鐢熸垚浜嗛暅鍍忥紝鐢?code>jlink --help
鏌ョ湅鏇村閫夐」銆?/p>
--patch-module
浠?Doug Lea 鐨?CVS 涓煡鐪?code>java.util.concurrent
鍖呬腑鐨?class 鏂囦欢鐨勫紑鍙戣€呭皢浼氫範鎯娇鐢?code>-Xbootclasspath/p
缂栬瘧婧愭枃浠跺拰閮ㄧ讲杩欎簺 class 鏂囦欢銆?/p>
-Xbootclasspath/p
宸茬粡琚Щ闄わ紝鍦ㄤ竴涓ā鍧椾腑锛岀敤鏉ヨ鐩?class 鏂囦欢鐨勬ā鍧楁浛鎹㈤€夐」鏄?code>--patch-module
銆傚畠涔熷彲浠ヨ鐢ㄤ簬澧炲姞妯″潡鐨勫唴瀹广€?code>javac
涔熸敮鎸佸湪缂栬瘧浠g爜鏃跺姞涓?code>--patch-module
閫夐」锛屸€滅姽濡傗€濇煇涓ā鍧楃殑涓€閮ㄥ垎涓€鏍枫€?/p>
杩欓噷鏈変竴涓紪璇戞柊鐗堟湰鐨?code>java.util.concurrent.ConcurrentHashMap
骞朵笖鍦ㄨ繍琛屾椂浣跨敤瀹冪殑渚嬪瓙锛?/p>
javac --patch-module java.base=src -d mypatches/java.base src/java.base/java/util/concurrent/ConcurrentHashMap.java
java --patch-module java.base=mypatches/java.base ...
鏇村淇℃伅
from: https://zhuanlan.zhihu.com/p/41129220
以上是关于Jigsaw 椤圭洰锛欽ava 妯″潡绯荤粺鏂版墜寮曞的主要内容,如果未能解决你的问题,请参考以下文章