Embeding the Scala Compiler in a Java program

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Embeding the Scala Compiler in a Java program

Keith Hughes
Hi folks,

I have a java project that includes a build system. The build system currently supports projects in java, but I would like to also compile projects that use the Java API the project exposes with both pure Scala projects, consisting of multiple files, and mixed Java/Scala projects with multiple files. I will need to set up a classpath to make sure the compiler gets all of the needed dependencies. 

The current system has embedded the Eclipse compiler for pure Java projects, but I really want to start using Scala more and more.

The Java code which decides which language is being used and then tries to use compilers in a programmatic fashion, rather than forking a process and running them that way.

Here, for example, is the method that calls the Java compiler:


You can see the method signature has a list of files to be compiled and a list of files that are the classpath that it picks up from its runtime environment.

Lines 85-87 then actually call the Java compiler API. I want to do something similar for Scala.

I don't know if it matters, all of this runs inside of an OSGi container.

If anyone is interested, this is for a platform for placing sensors in a physical space and processing the data, causing events to happen in the physical space. See http://www.github.com/smartspaces-io

Best,
-Keith

--
You received this message because you are subscribed to the Google Groups "scala-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Embeding the Scala Compiler in a Java program

Lukas Rytz-2
Hi

This might help to get you started: https://github.com/scala/scala/blob/2.12.x/test/junit/scala/tools/testing/BytecodeTesting.scala#L157
The file contains some additional utils to compile scala code in-memory and for testing.

Lukas



On Saturday, June 4, 2016 at 6:07:48 PM UTC+2, Keith Hughes wrote:
Hi folks,

I have a java project that includes a build system. The build system currently supports projects in java, but I would like to also compile projects that use the Java API the project exposes with both pure Scala projects, consisting of multiple files, and mixed Java/Scala projects with multiple files. I will need to set up a classpath to make sure the compiler gets all of the needed dependencies. 

The current system has embedded the Eclipse compiler for pure Java projects, but I really want to start using Scala more and more.

The Java code which decides which language is being used and then tries to use compilers in a programmatic fashion, rather than forking a process and running them that way.

Here, for example, is the method that calls the Java compiler:

<a href="https://github.com/smartspaces-io/smartspaces/blob/master/workbench/smartspaces-workbench/src/main/scala/io/smartspaces/workbench/project/java/EclipseProjectJavaCompiler.java#L70" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fsmartspaces-io%2Fsmartspaces%2Fblob%2Fmaster%2Fworkbench%2Fsmartspaces-workbench%2Fsrc%2Fmain%2Fscala%2Fio%2Fsmartspaces%2Fworkbench%2Fproject%2Fjava%2FEclipseProjectJavaCompiler.java%23L70\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG5s8jT047e1dPLP-9NusmHVAyLFg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fsmartspaces-io%2Fsmartspaces%2Fblob%2Fmaster%2Fworkbench%2Fsmartspaces-workbench%2Fsrc%2Fmain%2Fscala%2Fio%2Fsmartspaces%2Fworkbench%2Fproject%2Fjava%2FEclipseProjectJavaCompiler.java%23L70\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNG5s8jT047e1dPLP-9NusmHVAyLFg&#39;;return true;">https://github.com/smartspaces-io/smartspaces/blob/master/workbench/smartspaces-workbench/src/main/scala/io/smartspaces/workbench/project/java/EclipseProjectJavaCompiler.java#L70

You can see the method signature has a list of files to be compiled and a list of files that are the classpath that it picks up from its runtime environment.

Lines 85-87 then actually call the Java compiler API. I want to do something similar for Scala.

I don't know if it matters, all of this runs inside of an OSGi container.

If anyone is interested, this is for a platform for placing sensors in a physical space and processing the data, causing events to happen in the physical space. See <a href="http://www.github.com/smartspaces-io" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.github.com%2Fsmartspaces-io\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEKeCw0RnlUsO4x7H1el3Lsyisvvw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.github.com%2Fsmartspaces-io\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEKeCw0RnlUsO4x7H1el3Lsyisvvw&#39;;return true;">http://www.github.com/smartspaces-io

Best,
-Keith

--
You received this message because you are subscribed to the Google Groups "scala-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Embeding the Scala Compiler in a Java program

Keith Hughes
Awesome, thanks Lukas!


On Mon, Jun 6, 2016 at 12:03 AM, Lukas Rytz <[hidden email]> wrote:
Hi

The file contains some additional utils to compile scala code in-memory and for testing.

Lukas



On Saturday, June 4, 2016 at 6:07:48 PM UTC+2, Keith Hughes wrote:
Hi folks,

I have a java project that includes a build system. The build system currently supports projects in java, but I would like to also compile projects that use the Java API the project exposes with both pure Scala projects, consisting of multiple files, and mixed Java/Scala projects with multiple files. I will need to set up a classpath to make sure the compiler gets all of the needed dependencies. 

The current system has embedded the Eclipse compiler for pure Java projects, but I really want to start using Scala more and more.

The Java code which decides which language is being used and then tries to use compilers in a programmatic fashion, rather than forking a process and running them that way.

Here, for example, is the method that calls the Java compiler:


You can see the method signature has a list of files to be compiled and a list of files that are the classpath that it picks up from its runtime environment.

Lines 85-87 then actually call the Java compiler API. I want to do something similar for Scala.

I don't know if it matters, all of this runs inside of an OSGi container.

If anyone is interested, this is for a platform for placing sensors in a physical space and processing the data, causing events to happen in the physical space. See http://www.github.com/smartspaces-io

Best,
-Keith

--
You received this message because you are subscribed to a topic in the Google Groups "scala-tools" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-tools/-SNUU_uloOs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Embeding the Scala Compiler in a Java program

Keith Hughes
Lukas,

Just to let you know, between your link and pouring over the scalac code, I finally figured it out. Thanks for your help.

-Keith


On Mon, Jun 6, 2016 at 9:55 AM, Keith Hughes <[hidden email]> wrote:
Awesome, thanks Lukas!


On Mon, Jun 6, 2016 at 12:03 AM, Lukas Rytz <[hidden email]> wrote:
Hi

The file contains some additional utils to compile scala code in-memory and for testing.

Lukas



On Saturday, June 4, 2016 at 6:07:48 PM UTC+2, Keith Hughes wrote:
Hi folks,

I have a java project that includes a build system. The build system currently supports projects in java, but I would like to also compile projects that use the Java API the project exposes with both pure Scala projects, consisting of multiple files, and mixed Java/Scala projects with multiple files. I will need to set up a classpath to make sure the compiler gets all of the needed dependencies. 

The current system has embedded the Eclipse compiler for pure Java projects, but I really want to start using Scala more and more.

The Java code which decides which language is being used and then tries to use compilers in a programmatic fashion, rather than forking a process and running them that way.

Here, for example, is the method that calls the Java compiler:


You can see the method signature has a list of files to be compiled and a list of files that are the classpath that it picks up from its runtime environment.

Lines 85-87 then actually call the Java compiler API. I want to do something similar for Scala.

I don't know if it matters, all of this runs inside of an OSGi container.

If anyone is interested, this is for a platform for placing sensors in a physical space and processing the data, causing events to happen in the physical space. See http://www.github.com/smartspaces-io

Best,
-Keith

--
You received this message because you are subscribed to a topic in the Google Groups "scala-tools" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-tools/-SNUU_uloOs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "scala-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Embeding the Scala Compiler in a Java program

Lukas Rytz-2
cool! sorry i only gave a rough answer, times are always busy..

On Fri, Jul 1, 2016 at 7:21 PM, Keith Hughes <[hidden email]> wrote:
Lukas,

Just to let you know, between your link and pouring over the scalac code, I finally figured it out. Thanks for your help.

-Keith


On Mon, Jun 6, 2016 at 9:55 AM, Keith Hughes <[hidden email]> wrote:
Awesome, thanks Lukas!


On Mon, Jun 6, 2016 at 12:03 AM, Lukas Rytz <[hidden email]> wrote:
Hi

The file contains some additional utils to compile scala code in-memory and for testing.

Lukas



On Saturday, June 4, 2016 at 6:07:48 PM UTC+2, Keith Hughes wrote:
Hi folks,

I have a java project that includes a build system. The build system currently supports projects in java, but I would like to also compile projects that use the Java API the project exposes with both pure Scala projects, consisting of multiple files, and mixed Java/Scala projects with multiple files. I will need to set up a classpath to make sure the compiler gets all of the needed dependencies. 

The current system has embedded the Eclipse compiler for pure Java projects, but I really want to start using Scala more and more.

The Java code which decides which language is being used and then tries to use compilers in a programmatic fashion, rather than forking a process and running them that way.

Here, for example, is the method that calls the Java compiler:


You can see the method signature has a list of files to be compiled and a list of files that are the classpath that it picks up from its runtime environment.

Lines 85-87 then actually call the Java compiler API. I want to do something similar for Scala.

I don't know if it matters, all of this runs inside of an OSGi container.

If anyone is interested, this is for a platform for placing sensors in a physical space and processing the data, causing events to happen in the physical space. See http://www.github.com/smartspaces-io

Best,
-Keith

--
You received this message because you are subscribed to a topic in the Google Groups "scala-tools" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-tools/-SNUU_uloOs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
You received this message because you are subscribed to the Google Groups "scala-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.