0

How does one add standard Java libraries to SBT. For example, my class relies on these imports:

import javax.mail._
import javax.mail.internet._
import java.util.Properties

Trying to compile with SBT fails if I use these, for example:

[error] /Users/jacobus/scalaprojects/doxy/src/main/scala/EmailService.scala:6: expected class or object definition
[error]   val props = new Properties();

Here's the source:

import java.util.Properties
import javax.mail._
import javax.mail.internet._

val props = new Properties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.debug", "true");
val session = Session.getInstance(props);
val message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, "[email protected]");
message.setSubject("This is the Subject");
message.setText("This is the Message");
val transport = session.getTransport("smtp");
transport.connect("localhost","username","password")
Transport.send(message);
4
  • 1
    The error doesn't seem to be relative to the import but rather to the lack of a class / object definition in your class. Might it be the case? Commented Feb 27, 2012 at 10:09
  • I wish it was, but the code runs fine in Idea. I just can't compile it with SBT Commented Feb 27, 2012 at 10:16
  • I've had this problem before where the Twitter Finagle libraries also depended on javax and could also not include it. Commented Feb 27, 2012 at 10:18
  • 3
    You get that error because you try to compile a scala script file as if it's a scala source file. In a scala source file all val statements must be inside a scala object or a scala class. A scala script file can be run with scala, but can't be compiled with scalac. Commented Feb 27, 2012 at 10:21

1 Answer 1

3

That is not compilable code: it's a script. A script can be run, but cannot be compiled.

Put that stuff inside a standard application declaration, like object MyApp extends App {, and you might get other errors, but not this one.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks D. I was having a blonde moment. That gets me the dufus award for the day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.