You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

独立模式使用SysML2 Xtext插件时触发PackageNotFoundException

问题:独立模式下SysML2 Xtext解析器实例化报错

我正在开发企业POC软件,计划以独立模式结合SysML2 Xtext插件与ANTLR语法,实现SysML2文件的解析、标记,最终完成令牌导航与简单代码生成。目前已成功构建各类JAR包并启动基础代码,但实例化Parser时触发以下错误:

org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'http://www.omg.org/spec/UML/20161101' not found. (classpath:/org/omg/sysml.xmi, 3, 100)

我推测是UML XMI在运行时无法加载,但不知道具体操作方式。同时找不到UmlStandAloneSetupGenerated这类类,我对EMF框架不熟悉,也未找到加载XMI并映射至URI的代码示例。

附测试代码:

package company.sysml2_tools;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.service.GrammarProvider;
import org.omg.sysml.xtext.SysMLStandaloneSetupGenerated;
import org.omg.sysml.xtext.parser.antlr.internal.InternalSysMLLexer;
import org.omg.sysml.xtext.parser.antlr.internal.InternalSysMLParser;
import org.omg.sysml.xtext.services.SysMLGrammarAccess;
import com.google.inject.Injector;

public class App {
    public static void main(String[] args) {

        try {
            String test = "package banana{}";
            InternalSysMLLexer lexer = new InternalSysMLLexer(new ANTLRStringStream(test));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            SysMLStandaloneSetupGenerated standalone = new SysMLStandaloneSetupGenerated();
            Injector injector = standalone.createInjectorAndDoEMFRegistration();

            InternalSysMLParser parser = new InternalSysMLParser(tokens, new SysMLGrammarAccess(
                    new GrammarProvider("org.omg.sysml", injector.getProvider(XtextResourceSet.class)), null));

        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
}    

更新记录:问题解决方法

经测试,可通过注册相关EMF包的方式解决该问题,代码如下:

UMLPackage umlPackageClass = UMLPackage.eINSTANCE;
TypesPackage typesPackage = TypesPackage.eINSTANCE;
StandardPackage standardPackage = StandardPackage.eINSTANCE;
SysMLPackage sysmlPackageClass = SysMLPackage.eINSTANCE;

内容的提问来源于stack exchange,提问作者gramm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:03:26