IntelliJ Idea Scala files not available in 'New' context menu - scala

I am new to both IntelliJ and Scala. I am attending the course "Functional Programming Principles in Scala" on Coursera. I downloaded the zip file for the sample assignment, which contained a sample Scala project.
I imported the project successfully (I guess) in IntelliJ. However, when I right-click on a package in the project explorer, there are no Scala-related templates. I can only select "New Java class", XML files and some forms.
Does anyone know why this happens and if there is another way to create a new Scala class or object from a template?
Thanks.

Import the project by selecting its build.sbt file - not the project folder, not the eclipse project file. Then everything is imported correctly.

For me it works as following:
Right clicked on src and selected "mark directory as" -> source root.

This may also be a caching issue.
If so, try File then Invalidate Caches / Restart

Yes, you need to make the scala directory a source directory (in Menu / Project structure / Modules ) but I also had to do the following:
Still in Project Structure goto Global libraries.
Select Scala SDK.
Right click the Scala SDK and Add to module...
Now my context menu gives me New / Scala class and so on.

Once you've created a project and sbt has finished initializing, try creating another project. It should show up correctly in the new project, as it worked for me. It seems to be some kind of caching issue though Invalidate Caches/Restart didn't help me. Also, ensure that source directories are marked properly (source folders appear in blue color, test sources are in green).

I had this problem everytime I created a new project with spaces in the name. eg "Hell Wev". Using "HellWev" as a project name seems to work fine

Same issue (and solution) as #cwadeevans.
A project named "Week 1" did not show Scala files in the context menu "New". The solution was to delete the project and create a new one named "Week1".

Related

Attaching sources in IntelliJ IDEA for scala project

I have Playframework 2 project with Scala (very small one). It uses Scala Anorm library.
I have the code like this:
package models
..
import anorm.SqlParser._
...
val rowParser = scalar[Long]
So, I would like to see source of scalar method. Trying to attache the sources I have in my plaframework source folder, but IDEA just swallows my request and does nothing back.
I use last version of IDEA and Scala plugin. Is it bug, probably?
First download source code :
1.) Click on this link : http://www.scala-lang.org/download/all.html
2.) Choose any scala version.
3.) Now in the last section 'Other Resources' you can see 'Sources' link. Click on it to download. In my case (https://codeload.github.com/scala/scala/tar.gz/v2.11.7)
Now point to this Source Code from IntelliJ.
1.) Open Project Structure in IntelliJ. Shorcut (Cmd + DownArrow)
2.) Select Global Libraries from the left section.
3.) Then on right side, Under 'Scala Library' section. Click '+' and point to source directory. See the screenshot.
Aha.. fixed.
So: I used to use 'attach source' in the top of the window editor - it does not work (I guess it is a bug - maybe related to the scala plugin, because usually it works).
But if try to attache sources to particular library - in "Project Structure -> Library -> +Attach File or Directories -> Sources" then it works.
Thanks.
To fix it we need to do some changes into IntelliJ IDEA platform. Most probably it will be fixed in IntelliJ IDEA 13.1.
I have the same problem. I installed "Scala Imports Organizer", and problem was solved.
for "automatically attaching source jars" when using Bloop and BSP you can add one line:
bloopExportJarClassifiers in Global := Some(Set("sources"))
to your build.sbt, also sbt updateClassifiers, also Reload all BSP Projects to refresh, also Invalidate Caches/Restart to trigger indexing (if not done automatically)
reference

How do you configure eclipse to work with a project that wasn't created by it (java)

I have been using the text editor gedit and the terminal for my latest project but I find that I have so many classes it would be more useful to be working with a separate directory for my .class and .java files so I can see what is going on more easily.
Unfortunately I have not been able to figure out how to get eclipse to work with a project that has already been made (or even one that was already made on eclipse with another computer!). This was not such a problem with two or three classes but I am getting to have so many that it is really tedious- It seems to want me to build a completely new project or nothing.
EDIT (additional info):
I don't think it will be possible to show the code of the project as I current have 12 classes for it but by the end I may have one or two more...
I know how to make a project in eclipse. What I am not sure how to do it open a directory full of .java files in eclipse that were created using a text editor rather than in an eclipse project and get eclipse to accept it as a project.
Does anybody know how to achieve this task?
You can import your project into eclipse doing the following steps:
Create a new Java Project in Eclipse (By going to File -> New -> Java Project) select a name for the project and you can select finish
You have to import your existing code to eclipse, go to:
File -> Import -> General -> FileSystem
Then browse to the directory where your code is currently, and say you want to import into the project you created in step 1
Afterwards remember to go to the project properties (right click in the project folder while in the navigator view, for example) and make sure the java build path has the right source folders configured.
Fortunately, you can use eclipse for any project previously created. Do you possibly have the code to show us how you are applying to the project in eclipse?
For example, you can create a project and class, then copy over starting from public static void main(String[] args) {
down to the bottom, then import the important stuff above the main class name.

Netbeans and creating JUnit tests

I am using Netbeans 7.0.1 with a web project that I have imported from existing sources. I have added the JUnit library to my project. In Netbeans tutorials online for version < 7 it says to create a JUnit test for a given existing class by righ-clicking on the source file in the project, select the menu "Tools" and then there should be an option to create a JUnit test. However this option does not appear for me. There seem to bean old bug report/request for functionality, describing this but it does not seem to have been resolved as it is mentioned as late as 2011-05-09 on the Netbeans bugzilla list (link Related on bugzilla). On the bugzilla it is suggested to explicitly create the folder /test/unit/src in the project, but still after this when I try to create JUnit test for an existing class from rightclick project -> new -> other -> JUnit, I get "no tests root folder was found in the selected project" (I tried changing the foldername to tests as well). Can I explicitly set this location in some configuration file and is there any way of getting the expected functionality allowing me to simply righclick a source file and select "JUnit test" to create a JUnit stub for the selected class ?
If you right click on the project and pick "Properties" you should see "Sources" on the left. Select that and then click "Add Folder..." under the "Test Package Folders" section. This will add a new test directory. If you have an existing one, delete it first (just make sure the test sources are saved somewhere else) and then add the new one.
If you right click on your project and add new Unit Tests-> JUnit Test it will automatically create the path for you.
if src/main/java is missing, netbeans 8 does not let you create test case in src/test/java using the wizard
looks like a bug, still valid in 2015
What kind of project you've made has a large effect on whether there's a test root already or no. In Netbeans 7.1.1, a "POM Project" doesn't create a test root directory as a part of the project build process. However, making a Java Application did make a test root by default.
I tried the directions offered by mamboking and I wasn't able to change the directories in the Sources section of the project properties. Making a new project of type "Java Application" make the correct test root.

How can I get Eclipse to index an entire C/C++ source tree that contains multiple "make projects"?

I'm having trouble getting Eclipse to index my source code. I'm using Eclipse Helios on Windows XP at the moment.
I have a directory called src. Within it, I have a bunch of dirs, something like this:
src
-include (common headers)
-lib
-libIO (source code for this library)
-libGUI (source code for this library)
-pgms
-pgm1 (source code and headers for this pgm)
-pgm2 (source code and headers for this pgm)
Each leaf has its own Makefile. There is no top-level Makefile in src. pgm1 can and does include files from include and lib/libIO and lib/libGUI.
Basically, I want Eclipse to index my entire src directory, without having to set up a C/C++ project for every leaf in my tree. I can't seem to find a way to make this happen.
Here's my symptoms; what I'm trying to solve for:
When editing source in the pgm1 dir, it references functions that are declared in my include dir header files and defined in source files found in lib/libIO.
However, when I press F3 to get to the declaration of a function, Eclipse says "Could not find symbol 'X' in index". I can't seem to get Eclipse to find either the header declaration or the source definition for the method under my cursor (like the Java module does perfectly). Is this possible?
I had exactly same issue as OP but for some reason the menus in Eclipse I was using (Helios Service Release 2) were not "Go to Properties -> C/C++ General -> Paths and Symbols -> Source Location.".
The way I fixed the issue was :
in C/C++ view, right-click on top project name;
in the menu, select New->C++ Project;
in the window that appears, select Convert to and then select C++ project;
click OK to close the window.
Eclipse will start indexing right away. Depending on the size of source tree it may take a while but you will have the indexing working finally after that.
I faced similar situation. I solved it in this way: right-click the project in project View-->select Index-->rebuild.
I managed to solve this thanks to comments here.... I ended up recreating my project. I used the Import method to download a project from CVS, and told it to use the New Project wizard to do so. When I got the New Project dialog, I told it this was a C++ Project, and the indexing now works fine.
I still wish I could index files without having to attach a specific project type to it, but at least I found an answer.
Thanks for the help all.
Exit Eclipse. In workspace go to ".metadata/.plugins/org.eclipse.cdt.core" and delete everything in there.
in the project explorer panel, right click the project you want to re-index, then select index, then select the action you want
Indexing of files and variables under different flags is always complicated when we have a huge project, lot of files and more importantly lot of different build options. I prefer playing around with the Indexer option to help me browse the entire code.
You can find it : Project Properties>C/C++ General/Indexer.
You can choose "Enable project specific settings"
Then it's up to you to choose the options you want for your project.
For a particular build we can choose "Use active build configuration" so that only the files and MACROS are considered which are used by the build script.
Or Index all source files in the entire project.
I am using Eclipse Neon 3, here's the solution that worked for me :
go to File
New
Convert to C/C++ Autotools project
then select your project and finish.
It can take some time to finish indexing, it depends on your project size.
I have experienced problems with the indexer of Eclipse Luna when there was an unresolved friend declaration in the class declaration - it seems then that the indexer skipped indexing the rest of that class, and all references to it was shown as unresolved by the indexer.
Solution: removed the unresolved friend declaration (in my case, it was legacy code that was no longer needed).
Please try the following (my project is set up differently, so I am not sure this will work for you).
Go to Properties -> C/C++ General -> Paths and Symbols -> Source Location.
Do you see your source folder there?
I observe this behavior:
When I add a source folder and then exclude it from build, it disappears from the above list. After that the folder is no longer indexed. Re-adding it to "Source Location" solves the problem: the folder is now indexed; remains excluded from build (as intended); is visible among source locations.
I believe it is a bug -- excluding a source folder from build should not remove it from source locations list.
In Coocox IDE (Eclipse + gcc) the problem is resolved by going from file menu
Edit > Preferences > C/C++ / Indexer > Build configuration for Indexer set to Active build configuration and the rebuild the project (Ctrl R).
If the project is already converted to C/C++ and still the index is not working you can right click on the project and Index and rebuild. The project will start indexing right away.
I am using
Eclipse IDE for C/C++ Linux Developers
Version: Helios Service Release 2
Build id: 20110218-0911
I followed the suggestions above and in addition I had to mark all referenced projects (with in the work space) using project properties->Project references
The following has worked for me in Eclipse Neon:
New Project -> C/C++ -> Makefile project with existing code -> Next. Then Navigate to the code and finish the project creation. Indexing starts automatically.

Editor does not contain a main type

Just going through the sample Scala code on Scala website, but encountered an annoying error when trying to run it.
Here's the code: http://www.scala-lang.org/node/45. On running it on Eclipse, I got this message 'Editor does not contain a main type' that prevents it from running.
Is there anything I need to do...i.e break that file into multiple files, or what?
In Eclipse, make sure you add your source folder in the project properties -> java build path -> source. Otherwise, the main() function may not be included in your project.
I have this problem a lot with Eclipse and Scala. It helps if you clean your workspace and rebuild your Project.
Sometimes Eclipse doesn't recognize correctly which files it has to recompile :(
Edit:
The Code runs fine in Eclipse
A simpler way is to close the project and reopen it.
You have to make sure that your .java files are in the .src folder in eclipse. I had the same exact problem until I got it figured out.
Just make sure that the folder you work in is added to the built path:
right-click your folder --> build Path --> Use as source Folder
and it should now find main therein.
You can try to run the main function from the outline side bar of eclipse.
I had the same problem. I tried all sorts of things. And I came to know that
My .java files were not linked and
they were not placed in the 'src' folder.
Things I did:
Project properties >> Java Build Path >> Source
Deleted the original 'src' folder which was empty using 'Remove' option
Added the source that contained my source .java files using the 'Add Folder' option
This solved the error.
Just close and reopen your project in Eclipse. Sometime there are linkage problems. This solved my problem
A quick solution:
First, exclude the package:
Right click on the source package >> Build Path >> Exclude
Then include it back:
Right click on the source package >> Build Path >> Include
What you should do is, create a Java Project, but make sure you put this file in the package file of that project, otherwise you'll encounter same error.
That code is valid. Have you tried to compile it by hand using scalac? Also, have you called your file "addressbook", all lowercase, like the name of the object?
Also, I found that Eclipse, for some reason, set the main class to be ".addressbook" instead of "addressbook".
you should create your file by
selecting on right side you will find your file name,
under that will find src folder their you right click select -->class option
their your file should be created
Make sure that your .java file is present either in the str package, or in some other package. If the java file with the main function is outside all packages, this error is thrown.
Have faced the similar issue, resolved this by right clicking on the main method in the outline view and run as Java application.
I just had this problem too. The solution is to make sure eclipse created the project as Java project. Just create a new Java project and copy your class into the src folder (and import the eventual dependencies). This should fix the problem.
The correct answer is: the Scala library needs to before the JRE library in the buildpath.
Go to Java Buildpath > Order and Export and move Scala library to the top
I had this problem with a Java project that I imported from the file system (under Eclipse Helios). Here's a hint: the src code didn't seem to be compiled at all, as no "bin" directory showed up.
I had to create a Java project from scratch (using the wizard), then compare the .project files of the non-working and working projects.
The project giving "Editor does not contain a main type" had this as the "buildSpec" in the .project file:
<buildSpec>
</buildSpec>
But the working project had this as the "buildSpec":
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
I copied this in, and the imported project worked.
I know my answer is for Java, but the same might be the issue for your Scala project.
May be the file you have created is outside the src(source) folder. Trying to call the class object(from the file located in the src folder) from the .java file outside the source folder results in the same error. Copy .java file to the source folder, then build it. The error will be gone.
I had the same problem. I had the main class out of the src package, in other folder. I move it in and correct folder and solved
run "eclipse -clean -refresh" from command line. This fixed the issue for me when all other solutions failed.
This could be the issue with the Java Build path.
Try below steps :
Go to project properties
Go to java Build Path
Go to Source tab and add project's src folder
This should resolve the issue.
If it is maven project please check the java file is created under src/main/java
If you are not getting please change the JRE path and create the java files in above folder structure
For me, in Eclipse 3.6, this problem occurs when my main method is not public. I caused the problem by having a main method like this:
static void main(String[] args)
The dubugger was unable to detect this by itself. I am pretty suprised Eclipse overlooked this.
In the worst case - create the project once again with all the imports from the beginning. In my case none of the other options worked. This type of error hints that there is an error in the project settings. I once managed to solve it, but once further developments were done, the error came back. Recreating everything from the beginning helped me understand and optimize some links, and now I am confident it works correctly.
Follow the below steps:
Backup all your .java files to some other location
delete entire java project
Create new java project by right click on root & click new
restore all the files to new location !!
File >> Import >> Existing Projects into Workspace >> Select Archive Filed >> Browse and locate file >> Finish. If its already imported some other way delete it and try it that way. I was having the same problem until i tried that.
One more thing to check: make sure that your source file contains the correct package declaration corresponding to the subdirectory it's in. The error mentioned by the OP can be seen when trying to run a "main type" declared in a file in a subdirectory but missing the package statement.
I have this problem too after I changed the source folder. The solution that worked for is just editing the file and save it.
Try 'Update Project'. Once I did this, The Run as Java Application option appeared.
In my particular 'Hello World' case the cause for this problem was the fact, that my main() method was inside the Scala class.
I put the main() method under the Scala object and the error disappeared.
That is because Scala object in Java terms is the entity with only static members and methods inside.
That is why Java's public static void main() in Scala must be placed under object.
(Scala class may not contain static's inside)