Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Exporting Parser+Generator as standalone jar file
Exporting Parser+Generator as standalone jar file [message #908976] Thu, 06 September 2012 06:34 Go to next message
mcse mcse is currently offline mcse mcse
Messages: 42
Registered: August 2012
Member
Hi All,
I have developed a grammar of my language via Xtext and subsequently written the code generator via Xtend.
Now, I want others to design their systems following my language and automatically translate their models into another model via the generator.
One can easily do this by using my parser+generator as plugin to eclipse.
However, my main concern is to let others use my parser+generator as standalone jar file. So, they can download such a jar file into their desktop and simply click it to be able design their models in my language and do translatation with the generator.

So, could you please let me know the instructions to follow in order to export my xtext(and xtend) framework as standalone jar file ?

Thanks a lot in advance.
Re: Exporting Parser+Generator as standalone jar file [message #908984 is a reply to message #908976] Thu, 06 September 2012 06:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi create a java main class that calls whatever you want and run it
once. Then use the export - java - runnable jar file wizard

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #909031 is a reply to message #908984] Thu, 06 September 2012 08:48 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcse
Messages: 42
Registered: August 2012
Member
Hi Christian,
First thanks a lot for the quick reply.
However, I would be glat if you let me know about any sample main class for this ? I mean that I do not have an idea about which class instance creates a lexer, which class instance creates a parser, which class instance creates the code generator, and how all of these are to be glued together ? That is, parser class instance is to receive as constructor input the tokens created by the lexer instance, and code genrator instance receives as input the parser the ast of the parser. However, which classes render these possible ?
Re: Exporting Parser+Generator as standalone jar file [message #909055 is a reply to message #909031] Thu, 06 September 2012 09:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi,

you can configure the generatorfragment to generate one for you.

~Christian
Re: Exporting Parser+Generator as standalone jar file [message #909116 is a reply to message #909055] Thu, 06 September 2012 11:09 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcse
Messages: 42
Registered: August 2012
Member
I am afraid I do not have any idea how generator package is configured so that runnable plugins can be possible.
Can you try to put some details about it ?
Re: Exporting Parser+Generator as standalone jar file [message #909122 is a reply to message #909116] Thu, 06 September 2012 11:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
As I said: have a look at the generator fragment in your language
workflow

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #912373 is a reply to message #909122] Thu, 13 September 2012 08:58 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcse
Messages: 42
Registered: August 2012
Member
Hi Christian,
To create my runnable jar file, I firstly created a new java launch configuration.
While creating Java launch configuration (via run Configurations-> Java Application -> new),
I specified the path of Main class as the path of the Main class in my Xtext project.
Then, for the project, I have selected "org.xtext.example.myProject".
For the arguments: -os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog .

Once I run this java configuration, I got the below error:

Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for '-os'; a registered resource factory is needed
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:394)
at org.eclipse.xtext.resource.SynchronizedXtextResourceSet.getResource(SynchronizedXtextResourceSet.java:23)
at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:50)
at org.xtext.example.mydsl.generator.Main.main(Main.java:32)

I think that I need to manually register resource factory, which was previously handled by the StandaloneSetupGenerated.java class.
However, I have no idea how to perform the registration manually which is essential if one need to run the project as runnable jar file.

Would you please guide me on this ?
Re: Exporting Parser+Generator as standalone jar file [message #912398 is a reply to message #912373] Thu, 13 September 2012 09:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi this should be part of your main method

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #912400 is a reply to message #912398] Thu, 13 September 2012 09:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
P.S: why do you have this -os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog .
stuff

this does not look like a java main class
Re: Exporting Parser+Generator as standalone jar file [message #912444 is a reply to message #912400] Thu, 13 September 2012 11:41 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcse
Messages: 42
Registered: August 2012
Member
Hi Christian,
I have just copied and pasted this from the arguments of the configuration that opens
eclipse editor when run. Do you think that I left the arguments empty for the Java Launch configuration ?


My Main.java has the following content:


public class Main {

public static void main(String[] args) {
if (args.length==0) {
System.err.println("Aborting: no path to EMF resource provided!");
return;
}
Injector injector = new org.xtext.example.mydsl.XcDStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
Main main = injector.getInstance(Main.class);
main.runGenerator(args[0]);
}

@Inject
private Provider<ResourceSet> resourceSetProvider;

@Inject
private IResourceValidator validator;

@Inject
private IGenerator generator;

@Inject
private JavaIoFileSystemAccess fileAccess;

protected void runGenerator(String string) {
// load the resource
ResourceSet set = resourceSetProvider.get();
Resource resource = set.getResource(URI.createURI(string), true);

// validate the resource
List<Issue> list = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
if (!list.isEmpty()) {
for (Issue issue : list) {
System.err.println(issue);
}
return;
}

// configure and start the generator
fileAccess.setOutputPath("src-gen/");
generator.doGenerate(resource, fileAccess);

System.out.println("Code generation finished.");
}
}

So, is what you are suggesting to add the following snippet inside the above main.java ? THe below snippet is obtained from "StandaloneSetupGenerated.java"

public Injector createInjectorAndDoEMFRegistration() {
// register default ePackages
if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("ecore"))
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"ecore", new org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl());
if (!Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xmi"))
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"xmi", new org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl());
if (!EPackage.Registry.INSTANCE.containsKey(org.eclipse.xtext.XtextPackage.eNS_URI))
EPackage.Registry.INSTANCE.put(org.eclipse.xtext.XtextPackage.eNS_URI, org.eclipse.xtext.XtextPackage.eINSTANCE);

Injector injector = createInjector();
register(injector);
return injector;
}

public Injector createInjector() {
return Guice.createInjector(new org.xtext.example.mydsl.XcDRuntimeModule());
}

public void register(Injector injector) {
if (!EPackage.Registry.INSTANCE.containsKey("...")) {
EPackage.Registry.INSTANCE.put("...", org.xtext.example.mydsl.myDsl.MyDslPackage.eINSTANCE);
}

org.eclipse.xtext.resource.IResourceFactory resourceFactory = injector.getInstance(org.eclipse.xtext.resource.IResourceFactory.class);
org.eclipse.xtext.resource.IResourceServiceProvider serviceProvider = injector.getInstance(org.eclipse.xtext.resource.IResourceServiceProvider.class);
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xcd", resourceFactory);
org.eclipse.xtext.resource.IResourceServiceProvider.Registry.INSTANCE.getExtensionToFactoryMap().put("xcd", serviceProvider);
}
Re: Exporting Parser+Generator as standalone jar file [message #912456 is a reply to message #912444] Thu, 13 September 2012 12:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi these are for eclipse. Standalone you need simply a model file name

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #912468 is a reply to message #912456] Thu, 13 September 2012 12:26 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcse
Messages: 42
Registered: August 2012
Member
Hi Chris,
Yes, that is true. What I have written above was in fact for runnning parser as eclipse editor.
So to run as standalone, I simply specified a file name, i.e., main.runGenerator("model.mydsl"); .
Once I run the java launch configuration, it works fine. Indeed, both the parser and the code generator works as expected.
The next step is to export this java launch configuration as runnable jar file.
I can create this jar file through export option of eclipse. However, how can I run this on windows or linux so that
people can specify model in my language and automatically generate their code too.
Re: Exporting Parser+Generator as standalone jar file [message #912493 is a reply to message #912468] Thu, 13 September 2012 12:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
????

Java -jar my.jar test.mydsl
????

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #912501 is a reply to message #912493] Thu, 13 September 2012 13:14 Go to previous messageGo to next message
mcse mcse is currently offline mcse mcse
Messages: 42
Registered: August 2012
Member
Hi Chris,
Admit that it was totally a silly question, that is why I have found out the answer two minutes later than I posted -- so very sorry for this !!!

I have just run the runnable jar in linux. Although the parser and translator work correctly, an exception is thrown.

0 ERROR ResourceValidatorImpl - The string resource '_UI_DiagnosticRoot_diagnostic' could not be located
java.util.MissingResourceException: The string resource '_UI_DiagnosticRoot_diagnostic' could not be located
at org.eclipse.emf.common.util.DelegatingResourceLocator.delegatedGetString(DelegatingResourceLocator.java:487)
at org.eclipse.emf.common.util.DelegatingResourceLocator.getString(DelegatingResourceLocator.java:392)
at org.eclipse.emf.common.util.DelegatingResourceLocator.getString(DelegatingResourceLocator.java:506)
at org.eclipse.emf.common.util.DelegatingResourceLocator.getString(DelegatingResourceLocator.java:498)
at org.eclipse.emf.ecore.util.Diagnostician.createDefaultDiagnostic(Diagnostician.java:94)
at org.eclipse.emf.ecore.util.Diagnostician.validate(Diagnostician.java:123)
at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:108)
at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:53)
at org.xtext.example.mydsl.generator.Main.main(Main.java:32)
Code generation finished.

Do you have any idea about what it is or where it is derived from ?
Re: Exporting Parser+Generator as standalone jar file [message #912507 is a reply to message #912501] Thu, 13 September 2012 13:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi no I have no idea what is message is about.

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #935898 is a reply to message #908984] Sun, 07 October 2012 08:47 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
Hello,
generator fragment created Main class for me. I can also export it as a jar file. Unfortunately, I am still getting following error:
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'C:\\bbb.mydsl'; a registered resource factory is needed


Where and how can I register resource factory? I checked, that line
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("mydsl", resourceFactory);

in the MyDslStandaloneSetupGenerated.java was called.

(I created new Xtext project with Greeting example, set "generateJavaMain = true" and expected that after .jar file is exported and called as "java -jar aaa.jar bbb.mysdl" it will work...)

Thank you & Best regards
Michal
Re: Exporting Parser+Generator as standalone jar file [message #935926 is a reply to message #935898] Sun, 07 October 2012 09:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Simply call yourdslstandalonesetup.dosetup()

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #935930 is a reply to message #935898] Sun, 07 October 2012 09:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
I gave it a try and it works out of the box

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #935955 is a reply to message #935926] Sun, 07 October 2012 10:07 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
Hello,

I have added this call into main function in Main class:
	public static void main(String[] args) {
		if (args.length==0) {
			System.err.println("Aborting: no path to EMF resource provided!");
			return;
		}
		Injector injector = new org.xtext.example.mydsl.MyDslStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
		Main main = injector.getInstance(Main.class);
		MyDslStandaloneSetup.doSetup();
		main.runGenerator(args[0]);
	}


But the problem is present anyway.
I try also call Main from Eclipse (Run as -> Java application). I set Program arguments to: "C:\bbb.mydsl" (and this file exists...)

I don't understand, what happens, when you say it works out of the box for you...

Thank you for any hint.
Best regards
Michal
Re: Exporting Parser+Generator as standalone jar file [message #935967 is a reply to message #935955] Sun, 07 October 2012 10:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi here is what I do
1 create new project with wizard
2 set generateJavaMain to true and run workflow
3 run the main class (as java app)

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #935997 is a reply to message #935967] Sun, 07 October 2012 11:08 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
I am lost in this, I did exactly the same you wrote.
I just added "C:\bbb.mydsl" to Program arguments in Run Configurations, otherwise it falls on "Aborting: no path to EMF resource provided!"
I use Xtext 2.2.1 but I don't think it matters.

Result (unfortunately):
Exception in thread "main" java.lang.RuntimeException: Cannot create a resource for 'C:\bbb.mydsl'; a registered resource factory is needed
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:394)
	at org.eclipse.xtext.resource.SynchronizedXtextResourceSet.getResource(SynchronizedXtextResourceSet.java:23)
	at org.xtext.example.mydsl.generator.Main.runGenerator(Main.java:51)
	at org.xtext.example.mydsl.generator.Main.main(Main.java:33)
Re: Exporting Parser+Generator as standalone jar file [message #936014 is a reply to message #935997] Sun, 07 October 2012 11:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi can you share your project?

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Exporting Parser+Generator as standalone jar file [message #936024 is a reply to message #936014] Sun, 07 October 2012 11:46 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
My project is available here:
zipped project: https://rapidshare.com/files/3158861793/eclipse_xtext_workspace.zip
example .mydsl file: https://rapidshare.com/files/767277021/bbb.mydsl
Thank you
Michal
Re: Exporting Parser+Generator as standalone jar file [message #936076 is a reply to message #936024] Sun, 07 October 2012 12:56 Go to previous message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6144
Registered: July 2009
Senior Member
Hi,

it works fine with this code on my machine

~Christian
Previous Topic:formatter doesn't remove empty lines
Next Topic:How can I autoload default imports from resources
Goto Forum:
  


Current Time: Thu Jul 31 11:43:00 EDT 2014

Powered by FUDForum. Page generated in 0.02759 seconds