为什么我的Java9模块服务不起作用?

时间:2018-12-07 15:02:46

标签: java service java-9 java-module module-path

// Set gris Size.
const gridSize = 100;

// Calculate and store several useful metrics.
const viewHeight = view.bounds.height;
const itemsCount = Math.floor(viewHeight / gridSize);

// For the simplicity of the demo, items are only drawn in one column so x
// coordinate is constant.
const x = view.center.x;

// Create original items to fill the screen.
let items = [];
for (let i = 0; i < itemsCount; i++) {
    items.push(createItem(i * gridSize));
}

// Center them on the screen.
project.activeLayer.position = view.center;

// On scroll...
view.element.onmousewheel = function(event) {
    // ...translate layer up or down.
    // Using layer translation instead of view scroll cause items coordinates
    // to be updated which will help us in later calculations.
    const translation = event.deltaY > 0 ? new Point(0, 10) : new Point(0, -10);
    project.activeLayer.translate(translation);

    // Trigger items addition process.
    addItemsIfNeeded();
};

// Simply create a random colored, horizontally centered circle, at given
// y position.
function createItem(y) {
    return new Path.Circle({
        center: new Point(x, y),
        radius: gridSize / 4,
        fillColor: Color.random()
    });
}

// Check for empty space at the bottom or the top of the page and create as
// many items as needed to fill the empty space.
function addItemsIfNeeded() {
    // Get extremas items y positions.
    const lastItemY = items[items.length - 1].position.y;
    const firstItemY = items[0].position.y;
    const deltaBottom = viewHeight - lastItemY;
    // If there is empty space at bottom...
    if (deltaBottom > gridSize) {
        // ...add items at bottom.
        const itemsNeededCount = Math.floor(deltaBottom / gridSize);
        addItems(itemsNeededCount, lastItemY);
    // If there is empty space at top...
    } else if (firstItemY > gridSize) {
        // ...add items at top.
        const itemsNeededCount = Math.floor(firstItemY / gridSize);
        addItems(itemsNeededCount, firstItemY, true);
    }
}

// Create given number of items and add them to the begining or the end of the
// stack.
function addItems(count, referenceItemY, before) {
    // For each new item...
    for (let i = 1; i <= count; i++) {
        // ...calculate y position...
        const y = before
            ? referenceItemY - i * gridSize
            : referenceItemY + i * gridSize;
        // ...create item...
        const item = createItem(y);
        // ...add it to the stack.
        if (before) {
            items.unshift(item);
        } else {
            items.push(item);
        }
    }
}

我正在使用IntelliJ IDEA运行此代码,但看起来子模块未运行。以下是程序的输出:

---------------------------------------------
package org.zpf.service;
public interface Services {
    void test();
}
module org.zpf.service.Services {
    exports org.zpf.service;
}
---------------------------------------------
package org.zpf.impl;
import org.zpf.service.Services;

public class Impl implements Services {
@Override
public void test() {
    System.out.println("Impl-1");
 }
}

module org.zpf.impl.Impl {
    requires org.zpf.service.Services;
    provides org.zpf.service.Services with org.zpf.impl.Impl;
}
----------------------------------------------
public class Demo {
   public static void main(String[] args) {
      ServiceLoader.load(Services.class).forEach(Services::test);
   }
}

module org.zpf.demo.Demo {
    requires org.zpf.service.Services;
    uses org.zpf.service.Services;
}

1 个答案:

答案 0 :(得分:1)

您需要做的就是 确保模块

module org.zpf.impl  // fixing the name from that of question

出现在模块路径上 。在命令行中执行您共享的命令,并添加impl模块的路径,按预期工作。

.../jdk-11.jdk/Contents/Home/bin/java -p .../Desktop/modular/out/production/demo:.../Desktop/modular/out/production/modular:.../Desktop/modular/out/production/impl -m org.zpf.demo.Demo/org.zpf.demo.Demo

打印expted输出

Impl-1

在您的 命令行 中(仅为便于阅读而设置)

-p /Users/tubetrue01/IDEA/Test/Demo/target/classes:
   /Users/tubetrue01/IDEA/Test/Services/target/classes

应修改为

-p /Users/tubetrue01/IDEA/Test/Demo/target/classes:
   /Users/tubetrue01/IDEA/Test/Services/target/classes:
   /Users/tubetrue01/IDEA/Test/Impl/target/classes

使用 IntelliJ IDEA ,您可以按照以下步骤进行操作:

  1. 项目结构>模块
  2. 选择Demo模块>导航到依赖项
  3. 添加依赖项[模态的左下角( + )]>模块依赖项
  4. 选择Impl模块并应用。
  5. 立即运行您的Demo类。