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 妯″潡绯荤粺鏂版墜寮曞的主要内容,如果未能解决你的问题,请参考以下文章

window绯荤粺-椤圭洰鎻愪氦鑷砱ithub

锛坖ava椤圭洰锛夊潶鍏嬪ぇ鎴?2.0

浠庨浂璧锋瀹炵幇闊充箰鎺ㄨ崘绯荤粺

姣曚笟璁捐涔?--- 鍩轰簬Spring Boot鐨勫湪绾块煶涔恮eb绯荤粺

甯︿綘浜嗚В椤圭洰绠$悊 | 椤圭洰绠$悊鍩虹鐭ヨ瘑