Using Joda Time in Scala (scalaj-time) with IntelliJ IDEA - scala

EDIT:
Version information, as of 01/27/2013:
Scala 2.10.0
IntelliJ IDEA Leda 123.139 (EAP)
Scala Plugin version 0.7.108 (Nightly Build)
JDK 7u11
Joda Time 2.1
ScalaJ-Time 2.9.1-0.6
I have given up on trying to work with SBT, as I didn't see any progress with it - I'm not sure how to combine IntelliJ and SBT, and after hours of trying, I had ended up where I'd first begun.
My JodaTime library contains the Scalaj-Time jar:
I have tried almost every combination of settings, reaching from Joint compilation (first Scala, then Java and vv), external build, over to lifting my JodaTime library over Scala library in the dependencies (yes, I've even done that), and invalidating caches. All I get is a bunch of errors - no matter if I "Make" the project, "Run" the current file or "Compile" it - that are produced randomly. And, sometimes, it works. Yes, as idiotic as it sounds, the behaviour is that undefined. I'm not sure as to what causes this. I have counted 3 different error messages that appear, I will upload them to pastie.org, otherwise it will take too much space: http://pastie.org/pastes/5887847/text.
What I have not tried: Going back to earlier releases of any of the apps/libraries vide supra, getting deep into SBT (I figured it's not worth it).
What do I expect from this thread? I expect that someone provide a solid solution to this problem which, in the best case, will not include any mention of SBT (optional).
Also, on a side note: Why does Joda-Time ship their library in a broken format? Is there any secret behind this that everyone should know about? What was the problem with including FromString into the release version? (I still think that's the cause of it all)
Regards,
Danyel
ORIGINAL POST: (you can ignore this)
I've got the latest build of IntelliJ (123.139), Scala 2.10.0, and hadn't encountered any problems thus far.
Instead of using Java time library, I was eager to use Joda Time, and seeing that there are a bunch of implicits to use in Scala, I wanted to try that out! I added all the libraries, but implicits wouldn't be recognized. When I tried to compile it via IDEA Popup Menu, following errors appeared:
error while loading DateTime, class file '~\workspace\Libraries\JodaTime\joda-time-2.1.jar(org/joda/time/DateTime.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 10 at byte 42)
error while loading DateTimeZone, class file '~\workspace\Libraries\JodaTime\joda-time-2.1.jar(org/joda/time/DateTimeZone.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 7 at byte 44)
error while loading AbstractInstant, class file '~\workspace\Libraries\JodaTime\joda-time-2.1.jar(org/joda/time/base/AbstractInstant.class)' is broken
(class java.lang.RuntimeException/bad constant pool tag 10 at byte 10)
I went to the source of those DateTime and noticed that org.joda.convert.FromString; was imported, but the resource didn't exist! I guessed it was because of that why Scala wouldn't compile. So I googled for what I should do. And everywhere it said "SBT, SBT!!", and I was trying to find out what this SBT was, so I installed it: Version 0.12.2, Manual Installation (I'm using Windows 7) from http://www.scala-sbt.org/. I wanted it to run with my IntelliJ so I went to ~\.sbt\plugins, and created a file build.sbt with the line addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0") and downloaded the zip from https://github.com/mpeltonen/sbt-idea, extracted it, and went to that very folder with CMD, entered sbt and gen-idea within sbt and it did a lot of things and all seemed to work out. So I went to my project that I wanted to run Joda Time with, and first of all, it seemed very hard to include Scala 2.10.0 to SBT. I created a file <PROJECT>\build.sbt and filled it with these lines:
name := "Project"
version := "1.0"
scalaHome := Some(file("C:/Program Files (x86)/scala"))
scalaVersion := "2.10.0"
Then, to include the scalaj-time, I went to <PROJECT>\project\project\ and created build.sbt with the line libraryDependencies += "org.scalaj" %% "scalaj-time" % "0.6".
Lastly, I went to <PROJECT> with CMD, entered "sbt", it (down)loaded a lot of files, and it seemed to work out, then I entered gen-idea, and everything was fine.
I opened IntelliJ and I could open the project easily. When I opened Project Structure, I went to Modules, and there were two Modules: Project and Project-build. I ignored the Project-build one, clicked on the Project module, and there were a lot of Source Folders and Test Folders that were not existent: src/main/scala etc., I removed them all and added my src as Source Folder.
I noticed that Joda-Time was not included in External libraries (btw I use Joda-Time 2.1 and Scalaj-time 0.6), so I had to add them myselves. The joda-time library is a .jar while the scalaj-time is a folder, but that should be no problem, right? I could run a simple println( DateTime.now ) instruction (I don't know if it was possible before; didn't try), but a + 2.months was still not possible. Here is a screenshot so you get the idea:
I think I explained in very detail how I proceeded and I hope that you can find something that I did wrong. I've been working for many hours to resolve this, but in the end, I landed where I was in the beginning...
Thank you for your time!
Regards!

How to import FromString for joda-time?
This worked like a charm (for now, who knows how things will ensue). I am so mad that I didn't think of this in the first place... Grrrrr.

EDIT:
This answer is invalid. See the EDIT in the main post. While this works SOMETIMES, it is still heavily undefined as to WHEN it works. When I create a new project, it varies from working to not-working (not compiling, printing random errors), but in another larger project, it had the same undefined behaviour, except now (for whatever reason) it doesn't work, PERIOD. (On my newly created project it still varies)
ORIGINAL POST:
So, after another 10 minutes of trying and googling, I found a .JAR for Scalaj-time (0.6).
I was like "Well, what harm can it bring?", downloaded it, and included it instead of the folder.
Tadaaa, everything worked... All I needed, too, was only these lines:
object Test extends App {
import org.scala_tools.time.Imports._
println( DateTime.now + 2.months )
}
No SBT, nothing, no other imports whatsoever. You just download scalaj_time from http://mvnrepository.com/artifact/org.scalaj/scalaj-time_2.9.1 (Version 0.6, as of now) and Joda Time (V 2.1), make a library ScalajTime that includes 2+ jars, and add it to your dependencies. All you need is that one import statement and you are ready to go.
Maybe the main problem was that I cannot import SOURCE Files from libraries? Because the source folder I imported seemed to have no impact whatsoever. I don't know how I "ignored" that fact but it seemed stupid to assume.
So, now it works.
Regards.

Related

Intellij Idea is loading *.class file instead of *.scala from library source

My problem is best described with a screenshot:
As you can see, there is a org.virtuslab:unicorn-core_2.11 dependency loaded in my SBT/Scala project. The jar file with corresponding sources is available as well. For some reason, Intellij Idea decided to ignore the available source file Table.scala and uses the .class file instead. Clearly, all other files are okay.
Obviously, without the information contained in the .scala file, Intellij is not able to properly analyze my code and I'm getting Cannot resolve symbol BaseIdRepository errors (and similar) all over my project.
I manually checked the source package and the Table.scala file is there and seems alright. Plus this worked flawlessly until two days ago. I don't know why it broke down. I tried to revert my project using git to an earlier version when this was still working, but no luck.
I tried to both downgrade and upgrade the library, I tried to remove it and add it again, I tried create a completely new project based on existing sources, but once again, no luck.
Just for the record, I'm able to compile and run my project. The only thing that might be related is this warning that I get when compiling the project:
[warn] /home/tobik/scala/payola-viz/src/app/model/appgen/repository/UsersRepository.scala:7: Could not find any member to link for "BaseIdRepository".
I got an update for the Scala plugin which seems to fix this issue.
In case you experience similar problems, this temporary solution worked for me: I added the unicorn sources directly to my code base and manually removed it from the project libraries. I got rid of the errors and it still compiles as the library remains as a dependency inside built.sbt. To avoid polluting my project, I added the included sources to .gitignore. The workaround broke after a while but all I had to do was repeat the procedure again.

Scala in IntelliJ error

I just downloaded the plug in for Scala in IntelliJ and have created a project but now have various errors I read that the problem can be that I am missing a library. But when I try and go to project structure -> dependencies to add a library I have no clue where in the files to look for a library.
The errors are really simple but I can't seem to figure it out.
Any suggestions would be helpful :)
Here's everything you need to properly set up Scala plugin in Intellij. Furthermore your code has several errors:
Are you sure you created a Scala project (when you did File->New->Project)? This looks to me like a Java project? That class file looks to me like a Java class not a Scala class (that's why you're getting compilation errors on def but not public class).
1) value is not defined anywhere, of course it will throw a compilation error
2) classes in Scala are by default public, you do not (cannot) mark them as such
You can save yourself a lot of trouble by creating a simple SBT project that specifies all of your dependencies, etc. and then just pointing the IDE at that. Then when you change build.sbt, IntelliJ IDEA will notice that and update itself automatically. Plus, your build.sbt gets checked into your code base so that anyone you are collaborating with sees your changes to the dependencies. And the project can be built in batch mode using sbt compile and friends.
The following page talks about IntelliJ IDEA's "SBT Import" feature:
https://confluence.jetbrains.com/display/IntelliJIDEA/Getting+Started+with+SBT
Scala source files end in .scala, not .java. Try renaming Counter.java to Counter.scala. This should improve things a lot.

How to compile just some files with sbt?

I've come pretty well along with sbt, the Scala Build Tool. If you only have small problems in the code, it's easy.
Now, after a major feature add, much of my code is broken and I sbt seems to be confused as to how the dependencies are. I could help it, compiling the fundamental modules first, but it does not seem to let me.
It's help system is... notorious.
> help compile
Compiles sources.
Yeah, well. I guessed that.
What I wanted to hear was: how do I compile only - say - src/module/A.scala.
This might not even be possible (hello again, make, never abandoned you!). At least I cannot find any reference on the Internet to applying sbt compile just to a single file.
I'm using sbt from the command line prompt, not an IDE.
UPDATE:
It was my fault. :/ Had split a source file into multiple, but forgot to copy a package clause to each of the new ones. Oooops.
Will keep this open for a while, since compiling just a single file (i.e. something like sbt compile filename) would imho not be a bad thing.
You could define a Multi-Project Build where the files you want to compile separately are encapsulated in a project. According to the docs, the following is then possible:
At the sbt interactive prompt, type projects to list your projects and project to select a current project. When you run a task like compile, it runs on the current project. So you don't necessarily have to compile the root project, you could compile only a subproject.
I just wanted to mention here that I came across sbt-compile-quick-plugin (https://github.com/etsy/sbt-compile-quick-plugin). It does what it says on the tin, just add addSbtPlugin("com.etsy" % "sbt-compile-quick-plugin" % "1.3.0") to your project/plugins.sbt, then you can just start up sbt and run compileQuick /path/to/your/file
(See https://stackoverflow.com/a/46849619/1358677)

How to debug the error "Class file needed by X is missing. reference value Y of package Z refers to nonexisting symbol" in Scala?

I am just getting started with Scala since last week and I am having this problem that is driving me crazy.
I have some code that I want to migrate fully from Java to Scala (except libraries) but I have hit a block where I can't now proceed because of this mysterious error.
Let's say that I have a class that I have defined in org.domain.subdomain.MyClass
I have a class that seems to have no compilation problems (as suggested by Intellij) but when I build my project's modules, I would get the following
Class file needed by MyClass is missing
reference value subdomain of package org.domain refers to nonexisting symbol.
I am certain (or almost?) that I have got the classpath right as the projects were compiling before I started rewriting the code in Scala.
I have not been able to get it to compile with the verbose option on. I am using FSC compiler as the project compiler. Intellij does not show anything with verbose option!
What is going on here?
How can I solve this?
I just had this problem, and reimporting the project in IntelliJ worked. I removed .idea directory and .iml file, then reimported the project as maven.
This is an IntelliJ issue - outside maven builds worked fine.
Your classpath is incomplete. The reference to org.domain.subdomain appears in a classfile referenced by one of our sources (as indicated by the error message).
If you are rewriting Java code, you may need add more of your transitive dependencies in each module's classpath. The Scala compiler is more eager when it comes to type-checking and in some situations it needs a larger classpath.
I know, the question is more than one year old, but maybe it helps someone.
I just had the same issue. One of my libraries used an newer version of org.scala-lang:scala-library than the main project. After the library update everything works fine.

How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?

I recently gave up trying to use Scala in Eclipse (basic stuff like completion doesn't work). So now I'm trying IntelliJ. I'm not getting very far.
I've been able to edit programs (within syntax highlighting and completion... yay!). But I'm unable to run even the simplest "Hello World". This was the original error:
Scala signature Predef has wrong version
Expected 5.0
found: 4.1 in .... scala-library.jar
But that was yesterday with IDEA 9.0.1. See below...
UPDATE
Today I uninstalled IntelliJ 9.0.1, and installed 9.0.2 Early Availability, with the 4/14 stable version of the Scala plug-in.
Then I setup a project from scratch through the wizards:
new project from scratch
JDK is 1.6.u20
accept the default (project) instead of global / module
accept the download of Scala 2.8.0beta1 into project's lib folder
Created a new class:
object hello {
def main(args: Array[String]) {
println("hello: " + args);
}
}
For my efforts, I now have a brand-new error :)
Here it is:
Scalac internal error: class java.lang.ClassNotFoundException [java.net.URLClassLoader$1.run(URLClassLoader.java:202), java.security.AccessController.doPrivileged(Native Method), java.net.URLClassLoader.findClass(URLClassLoader.java:190), java.lang.ClassLoader.loadClass(ClassLoader.java:307), sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301), java.lang.ClassLoader.loadClass(ClassLoader.java:248), java.lang.Class.forName0(Native Method), java.lang.Class.forName(Class.java:169), org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java:72)]
FINAL UPDATE
I uninstalled 9.0.2 EA and reinstalled 9.0.1, but this time went with the 2.7.3 version of Scala rather than the default 2.7.6, because 2.7.3 is the one shown in the screen-shots at the IntelliJ website (I guess the screen-shots prove that they actually tested this version!). Now everything works!!!
I have encountered the same scalac error when trying to run a Scala project in Intellij Idea 9.0.2 and I've managed to find a solution by chance :). These are the steps I took in creating the project and running it.
I have created a Scala project in Intellij Idea 9.0.2 final (it was released today). I have installed the Scala plugin, restarted the IDE and created a new Scala project (with the name "TestScala") with scala-2.8.0.Beta1 as project library. Once the project is created and the scala libraries downloaded, I have created a Test.scala file with the following content:
object Test {
def main(args:Array[String]){
println("hello")
}
}
After that, I created a launch configuration ("Edit Configurations"), choosing the "Application" template. I set as main class Test and choose the project name ("TestScala") in the "Use classpath and JDK of module" combo box. When I run the configuration I get the same error as you reported ("Scalac internal error: class java.lang.ClassNotFoundException") .
Now comes the freaky part :). I right click on the project, choose "Module Settings", have a look on all settings but I don't change anything . Click "apply" and "ok", try to run configuration again and it works :) .
I use Intellij Idea 9.0.2 the final release (build 95-66); Ubuntu 9.10 and JDK 1.6.0_18. I also have to mention that I had a JDK configured in Intellij, otherwise there is an extra step to configure it.
UPDATE:
When checking the setting of the module, one needs to click on the Module->Scala and Facets->Scala (expand it and click on Scala(ProjectName)) . Both of these settings are about the scala compiler and scala library location. I would guess these values are not properly set when the project is created but are saved once the user touches them and saves the settings.
To answer your question, it's difficult to get a working IDE for Scala for two reasons:
(a) Scala is only just beginning to reach a wide audience and
(b) due to (a), there is no business case for spending time on a Scala IDE.
Also, if you are old enough to cast your mind back and young enough to still remember, you would know that for the first five or more years of Java, we were stuck with okay-ish tools like JBuilder that did little more than compile your code when you said so - no error highlighting, no auto-importing, and the word refactoring didn't even exist. If you want to pioneer, you need to be prepared to cut some of the road yourself, or at least bush-bash.
I know it won't help you, but I have successfully used IDEA for Scala on Linux, Mac and Windows. I typically have the Scala SDK installed somewhere locally and point IDEA at that rather than using the 'download' option.
Presently, I am mostly using an EAP version of IDEA 9 on Mac OS X with Scala 2.8.0.Beta1-RC5 and it's working well (except that fsc doesn't seem to worked with mixed sources).
You could try your luck over at the IDEA Scala Plugin Discussion Forum, though I haven't had a great lot of responses to my own postings there.
Installing the plug-in is prerequisite one.
The next thing you should do is define a library (global or project-specific; I use global) that holds the Scala library and compiler JAR files (at a minimum, that's scala-compiler.jar and scala-library.jar). Adding source JARs and a documentation JAR or URLs is a good idea, too. Then make this library a dependency of any modules in your project that include Scala code.
Lastly, find the Scala facets in those modules and de-select both check-boxes there.
I just did a fresh install and had exactly this same problem myself.
It turned out that, because I had created the file in the root package, IDEA had added a package statement at the top with naming a package. I assume that this then got compiled as "package object Main" - valid syntax in 2.8? Anyway, I deleted the line that said package and it all worked fine.
I had the same problem yesterday while trying to set it up. Solution is pretty simple, you just have to set scala somewhere in project settings.
You are mixing code compiled with two different Scala versions.
I use Netbeans to write scala programs. So far it works very well with my codes. You can try the plugin here: http://wiki.netbeans.org/Scala68v1.
I was getting this error and also had to right click on the project and "Open Module Settings". However, it was more than just hitting apply. I had to make sure that my Content Root was correct for each project. For some reason, there were some incorrect Source and Test Folders.
My project uses maven as the main build tool and importing the project into Intellij is probably what created these incorrect settings.
I had similar problem, following this blog post instructions solved the problem for me