如何识别特定包的jar文件

时间:2014-09-03 09:01:40

标签: java swing jar package

我使用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;
}

1 个答案:

答案 0 :(得分: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” - 你错过了最后一次“行动”。

您可以使用标准的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'的类。