gpt4 book ai didi

java - 如何识别特定包的 jar 文件

转载 作者:行者123 更新时间:2023-12-01 12:35:28 25 4
gpt4 key购买 nike

我使用 swing 组件和 JAPI 编写了一个简单的编辑器。当我运行该程序时,它显示一些错误。我将 JAPI jar 文件 (japi-lib-swing-action-0.3.0.jar) 添加到我的项目库中我正在使用 NetBeans IDE。它显示以下错误“无法解析 import 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;
}

最佳答案

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

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

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

我正在再次查看您的问题,您说

"the import net.sf.japi.swing.ActionMethod cannot be resolved "

我认为你的包名称错误,它应该是“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”的类。

关于java - 如何识别特定包的 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25640276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com