【问题标题】:How to identify a jar file for particular package如何识别特定包的 jar 文件
【发布时间】:2014-10-27 17:10:15
【问题描述】:

我使用 swing 组件和 JAPI 编写了一个简单的编辑器。当我运行程序时,它会显示一些错误。我将 JAPI jar 文件 (japi-lib-swing-action-0.3.0.jar) 添加到我的项目库中文件夹。我正在使用 NetBeans IDE。它显示以下错误“无法解析导入 net.sf.japi.swing.ActionMethod”。哪个 jar 文件包含该包。

这里是示例代码:

import org.jetbrains.annotations.Nullable;
import net.sf.japi.swing.ActionFactory;
import static net.sf.japi.swing.ActionFactory.getFactory;
import net.sf.japi.swing.ActionMethod;
import net.sf.japi.swing.ActionProvider;

public class Editor implements ActionProvider {
/** Action Factory. */
private static final ActionFactory ACTION_FACTORY =  getFactory("net.sf.japi.examples.editor");

/** The supported editor action names and their corresponding kit action names. */
private static final Map<String, String> editorActionNames = new HashMap<String, String>();
static {
    editorActionNames.put("editCut",       DefaultEditorKit.cutAction);
    editorActionNames.put("editCopy",      DefaultEditorKit.copyAction);
};

/** Application frame. */
private final JFrame frame = new JFrame(ACTION_FACTORY.getString("frame.title"));

/** Editor component. */
private final JTextPane textPane = new JTextPane();

/** FileChooser for opening and saving files. */
private final JFileChooser fileChooser = new JFileChooser();

/** Currently opened file.
 * Maybe <code>null</code> in case the current document was not already saved.
 */
private File file;

/** Create the Editor. */
public Editor() {
    ACTION_FACTORY.addActionProvider(this);
    frame.setJMenuBar(ACTION_FACTORY.createMenuBar(true, "editor", this));
    frame.add(ACTION_FACTORY.createToolBar(this, "editor"), NORTH);
    frame.add(new JScrollPane(textPane));
    frame.pack();
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}

/** {@inheritDoc} */
@Nullable public Action getAction(final String key) {
    for (final Action action : textPane.getActions()) {
        final String realKey = editorActionNames.get(key);
        if (realKey != null && realKey.equals(action.getValue(Action.NAME))) {
            ACTION_FACTORY.initAction(true, action, key);
            return action;
        }
    }
    return null;
}

/** Action method.
 * @used
 */
@ActionMethod public void fileNew() {
    textPane.setText("");
    file = null;
}

【问题讨论】:

    标签: java swing jar package


    【解决方案1】:

    您需要将相关的 jar 添加到 Netbeans 中的类路径/构建路径。检查您的项目设置。

    我可以确认japi-lib-swing-action-0.3.0.jar中确实存在ActionMethod:

     $ jar tf ~/../Downloads/japi-lib-swing-action-0.3.0.jar | grep ActionMethod
    net/sf/japi/swing/action/ActionMethod.class
    

    我正在再次审查你的问题,你说

    “导入net.sf.japi.swing.ActionMethod无法解析”

    我认为你的包名有误,应该是“net.sf.japi.swing.action”——你错过了最后一个“action”。

    您可以使用标准的 zip 文件提取程序(例如 winzip 或 7-zip)来检查 jar 文件的内容。

    根据我的经验,Java 在识别哪些 jar 包含哪些类(甚至是包)方面做得很差。我使用这样的 shell 函数,它几乎可以完成这项工作:

    function findJar {
    
      basedir=$1
      searchString=$2
    
      find $basedir -type f | egrep "(\.jar|\.war|\.ear)$" | while read jarFile; do
    
        jar tf $jarFile | grep $searchString > /tmp/findJar.tmp
    
        if [[ $? -eq 0 ]]; then
    
          echo " *** $jarFile"
          cat /tmp/findJar.tmp
        fi
      done
    
      rm /tmp/findJar.tmp 2> /dev/null
    }
    

    然后你可以像 findJar $my_jar_dir ActionMethod 这样调用它,它应该告诉你哪些 jar 包含一个名为“ActionMethod”的类。

    【讨论】:

    • ActionMethod 已解决。但是,它显示 ActionFactory 方法的包“net.sf.japi.swing”不存在。请也检查它。
    • 是的,你需要在'net.sf.japi.swing'之后添加'action':'net.sf.japi.swing.action'。
    • 您可以使用标准的 zip 文件提取程序,例如 winzip 或 7-zip 来检查 jar 文件的内容。
    • 哦,是的,我添加了一个。但是,ActionFactory 类在该包中不可用。
    • 哈,我下载解压了,但是这个包没有ActionFactory类,请检查一下,它有ComponentFactory类。
    猜你喜欢
    • 2011-11-09
    • 2014-12-12
    • 1970-01-01
    • 2014-08-25
    • 2010-09-28
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2019-11-08
    相关资源
    最近更新 更多