【发布时间】:2011-10-24 18:13:35
【问题描述】:
我发现需要在我的项目中使用 String.format。但是,这在 GWT 中不起作用,我有一个使用 GWT RegExp 的替代品。但是,我希望我的项目在使用不依赖于 GWT 的普通 Swing 接口时能够正常运行。不过,共享代码取决于 String.format。问题是我不知道如何让 GWT 编译器使用与其他代码不同的类。我已经尝试在一个文件中完成所有操作,但它不起作用,因为 GWT 无法处理 NoClassDefFoundError 异常,我使用该异常来检测 GWT 何时从类路径中丢失。
这是我当前的代码:
package testpackage.shared;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.regexp.shared.SplitResult;
public class Util {
public static String format(final String format, final Object... args) {
try {
final RegExp regex = RegExp.compile("%[a-z]");
final SplitResult split = regex.split(format);
final StringBuffer msg = new StringBuffer();
for (int pos = 0; pos < split.length() - 1; pos += 1) {
msg.append(split.get(pos));
msg.append(args[pos].toString());
}
msg.append(split.get(split.length() - 1));
return msg.toString();
} catch (NoClassDefFoundError ex) {
return String.format(format, args);
}
}
}
如何在没有 GWT 的类路径中编译和运行 GWT 和 JRE?在为 GWT 和 Swing/Sun JRE 构建时,我真的需要让 Ant 构建脚本替换整个文件吗?
【问题讨论】:
-
你说 String.format “在 GWT 中不起作用”,有什么问题?
标签: java gwt ant noclassdeffounderror