How do I get rid of "Current text selection cannot be opened in an editor" in Eclipse? - eclipse-jdt

Sometimes, I get this message in the status bar when I press F3 on a Type in a Java editor:
Current text selection cannot be opened in an editor
When I copy the selection into the clipboard and use Ctrl+Shift+T to open the "Open Type" dialog, I can paste the value and it will display the type and I can open it. The type is on the class path (no compile errors).
In other Java editors in the same project, F3 / Ctrl-Click works.
In the "broken" editor, it only works on internal fields and methods. For internal fields, it only works in the place where the field is defined. All places where a field is used are dead. "Mark Occurrences" works, though.
How do I get rid of it?
So far, I tried:
Close and open the editor
Clean the project
Clean all projects
"Update Project..." in the Maven context menu
Restart Eclipse
Restart Eclipse with -clean
This happens on Eclipse 3.7.2 with m2e 1.3.1 installed.

[UPDATE] This seems to be a bug somewhere in the depths of m2e and JDT. Usually, it appears when you have several projects (Maven multi module / reactor build) and the modules MA, MB and MC depend on each other:
MC depends on MB depends on an inner parameterized type X.Y<T> of MA.
and you have closed the module MB. On the classpath of MC, this will look like so:
MB.jar
MA
That is MB exists as a JAR on the classpath while MA is imported as an Eclipse project from the workspace.
In this situation, Eclipse gets confused when reading .class files in MB.jar which need inner parameterized types from MA. My guess is that it needs the type from MB.jar, parses it which tells it of the dependency to the type in MA which it then tries to parse only to find that the type parameter for T can't be resolved since parsing of MB isn't finished, yet.
There are three fixes:
Close MA. That will import everything as a JAR.
Open the project MB.
Make the inner type a toplevel type
[OLD]
If this happens, try these steps in order of increasing despair:
Close the editor and open it again.
Clean the project
Close the project and open it again.
Restart Eclipse
If all that fails:
Export your preferences
Exit Eclipse
Delete (or rename) the folder .metadata in your workspace. It's an invisible folder on some OSs but it's there.
Start Eclipse again
Import your preferences
Import all projects again. For this, select the workspace. Eclipse will then list all projects in the dialog and you can select all of them at once.
Related:
Bug 430605 - [select] Current text selection cannot be opened in an editor

There is a bug in Eclipse (in Luna at least) for a very specific workflow, which might not be the answer to this specific question, but might be helpful for others.
If you perform an import into the src (meaning src folder has focus before performing import) folder from an extracted jar (as source code), and you place breakpoints in the nested *.java files in the nested packages, it won't hit those breakpoints. In order to get things to work, you must keep hitting F3 instead of using "Open Declaration" to get to the *.java file you want to set a breakpoint in, then add the breakpoint by double clicking in the far left margin. Then Run > Debug As > Java Application.

this issue comes because eclipse workspace created by eclipse unable to load source code properly into its cache
please follow below steps to resolve this issue
goto eclipse workspace
for eg
i have loaded source code on below path:
C:\Users\ABCD\WorkingSourceCode7july (it will have .metadata)
just remove current working folder (WorkingSourceCode7july)
after that open eclipse again and create new working folder(WorkingSourceCode7july) and try to import same source code
i hope this may resolve the issue
this worked for me

Related

Make PyCharm recognize string as path

I am working with Python 3.4.
When I use IDLE and start typing, e.g.,
my_main_folder = "C:/Us"
in the editor window, and then press Ctrl + Space, IDLE offers me all subfolders of C:, and if I press Tab, the string is completed to "C:/Users".
That means that IDLE (or Python Shell) is able to recognize a string as a path.
How to make PyCharm (Community edition) do that?
Its not able in Community Edition Pycharm. But it can be done in Professional Edition Pycharm. I would suggest you to move to Professional Edition.
Does PyCharm have autocomplete file path?
BUT,There is a work around for it in Community Edition.
Place caret in such place and press Alt+Enter -- see if you will have "Inject language or reference" option in appeared menu. If it's present -- use it and choose "File Reference" there.
NOTE 1: this option may not be available if string is empty or has less than 2 characters in it
NOTE 2: this is temp injection -- AFAIK it will be lost after IDE restart (and you will need to do it again if needed)
Otherwise: select desired file in Project View panel and use "Copy Reference" -- it will copy a file path relative to the project root -- you may only need to add leading "/"...
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206574619-Autocompleting-relative-paths
After searching I found this answer:
can we tab through paths, in the same way you can in the terminal?
No, it's not posible:(
I wrote a plugin for Komodo called AutoCode that allowed me to ALT+Click on files in the Project view, and it would automatically resolve the right path
You can use 'Copy reference' in file right-click menu to copy file path relative to project root to clipboard. But this action doesn't unfortunately take resource roots into account
However, you can use CNTRL-SPACE twice and it will present a list of paths and files, example:
For a detailed description of this information see here

Cannot find the create package menu in Android Studio

I have a trouble to add a package under one of the flavor folder in Android Studio: after I right click on the java folder and I could not see the 'Package' or 'Java Classes' options there!
I did exactly the same for another flavor and it was fine.. Now I am stuck here, without knowing if it is a bug in AS or I did anything there. Anyone help me please!
(I googled it and did not solve it with what I have found so far)
=======EDIT========
I close the project and create a brand new empty project, now even in the main folder I cannot find the 'Java Classes' or 'Package' when I right click on the java folder I added.
However, if I just create the folders of the package under java with a class there, and do a build, it will finally recognize that and make java blue color and then I am able to see the menus if I right click the blue java folder.
So it sounds like some bug in AS.
It might just be the build flavor you are currently selected correctly to enable that flavor.
If we have two build flavor let's say free and pro. If pro is selected is current build flavor. The flavor free will not be shown as java package in blue color as you mentioned, it will be shown as directory structure. Hence change the build variant and make it workable.
Not sure if you faced the same issue, but sharing it for reference of others who might face this issue as i faced now.
Package option is available only for selected flavor. switch to desired flavor and IDE will refer that source directory and will convert it to package and directories from other flavors will be listed as normal directories tree structure.
I found this Solution.
File > Project Structure > Select the lib folder > Click on Sources > Apply and Okay
Now you can see the new package option by right clicking the lib folder, the new package option will be shown.
Step1
Step2
Step3
Control click (mac), or right click the /java directory and select Mark Directory As -> Source Root. You should then be able to right click the /java directory and select -> New -> Package. Your java directory will also appear blue.
Solution. File- Project Structure- Modules- Right click on lib - Select Sources.
then the option "Package" will appear on your list.

Rename Android Studio project causing a bunch of errors and java files missing

Following some links here I tried to rename a project. I went to c:\path\to\AndroidStudioProject and renamed the folder there, then found the .idea file and changed that. Now the project loads but java files are missing! However, on clicking java (in the bar at the top), then com I can see them each with a small red j.
I then looked in module Gradle build where there were 3 files, one with the new name and 2 older ones which stated that they did not belong so I deleted them, copying the compile info into the new one. Naturally on doing a Sync Now I get a bunch of errors.
Why does this have to be such a complex operation? Can anyone assist?
After you rename the folder, open the project in Android Studio. From there, right click something that needs to be renamed (e.g., module, package names, etc.) and use Refactor->Rename from the context menu. Keep doing this until everything that can be refactored is.
You will need to manually change the value of applicationId in the app-level build.gradle. Obviously, things like #string/app_name will also have to be changed manually. I recommend making these manual adjustments from inside Android Studio rather than directly editing the files. That way you'll know immediately if you break something.

No index in Eclipse CDT project checked out from SVN

I have checked out a cross-compilation project from SVN on Eclipse (on a Linux system). I am unable to open a functions declaration from the functions's right-click context menu (or by pressing F3). Upon research, I found out I don't even have anything like Index -> .. in the project's right-click menu. I have tried a lot with the Preferences -> C/C++ -> indexer but no useful results. I am not using Eclipse to build it but there are no errors since I can build it using makefile
PS: Though there are a few questions with the matching title, but none of them helped me
Here is the solution found after a million attempts. The project should be checked-out as C/C++ project, by selecting import using new project wizard.. The index will NOT be available in the SVN projects checked-out directly. Hope it saves some one's time
Eclipse Kepler SR2 on Windows 7 here.
Faced the same issue, the following sequence of inputs made it work for me:
Import...
"Checkout projects from SVN."
Select branch etc.
Check the "Check out project configured using the New Project Wizard" radio button.
Select "C/C++ -> C Project."
Pick a toolchain, doesn't matter which if you use an external one.
Now you can right click on the project (once fully checked out) and rebuild the indexer.
I am using Indigo and solved it as follows (without checking out again!)
Right click on project in question and select New->Convert to a C/C++ Project
After selecting toolchain, indexing gets built automatically.

Can Android Studio be used to run standard Java projects?

For those times when you want to isolate the Java and give it a quick test..
Can you run non-Android Java projects in Android studio as in Eclipse?
Tested on Android Studio 0.8.6 - 3.5
Using this method you can have Java modules and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects.
Open your Android project in Android Studio. If you do not have one, create one.
Click File > New Module. Select Java Library and click Next.
Fill in the package name, etc and click Finish. You should now see a Java module inside your Android project.
Add your code to the Java module you've just created.
Click on the drop down to the left of the run button. Click Edit Configurations...
In the new window, click on the plus sign at the top left of the window and select Application
A new application configuration should appear, enter in the details such as your main class and classpath of your module.
Click OK.
Now if you click run, this should compile and run your Java module.
If you get the error Error: Could not find or load main class..., just enter your main class (as you've done in step 7) again even if the field is already filled in. Click Apply and then click Ok.
My usage case:
My Android app relies on some precomputed files to function. These precomputed files are generated by some Java code. Since these two things go hand in hand, it makes the most sense to have both of these modules in the same project.
NEW - How to enable Kotlin in your standalone project
If you want to enable Kotlin inside your standalone project, do the following.
Continuing from the last step above, add the following code to your project level build.gradle (lines to add are denoted by >>>):
buildscript {
>>> ext.kotlin_version = '1.2.51'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
>>> classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
...
Add the following code to your module level build.gradle (lines to add are denoted by >>>):
apply plugin: 'java-library'
>>> apply plugin: 'kotlin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
>>> implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
>>> runtimeClasspath files(compileKotlin.destinationDir)
}
...
Bonus step: Convert your main function to Kotlin! Simply change your main class to:
object Main {
...
#JvmStatic
fun main(args: Array<String>) {
// do something
}
...
}
EDIT: many moon after this question was asked, yes, now apparently you can.
No, but it's based on Intellij IDEA.
The community version of that is free for download but it doesn't support most things requiring an external database or application server. The line for Java is pretty much that JavaSE code can happily use Community.
If you want that (or are using JavaEE) then you either need the Ultimate version, which isn't free, or the EAP of the next version which is usually good for a month until they release another.
Basically it works like this
Android Studio is just Android the Android Stuff from IDEA 13 Community...
...which will be free, and is anything from IDEA 13 Ultimate...
...that doesn't require a database or app server.
http://www.jetbrains.com/idea/
IDEA 12 Community doesn't have the ability to import the gradilized project and it won't, so while you can do Android development in it now (I do), don't expect it to have the same features as Android Studio. There's a lot of good new Android stuff in it, that's going into 13.
Easy way to run a java program in Android Studio would be,
Create a java Class says "Test.java" in Android Studio.
Write your code eg, a Hello World program to test.
Right-click on the Java class and:
select the option Run 'Test.main()'
or
press CTRL + SHIFT + F10 (on windows) or control + R (on Mac)
There you have your Java code running below.
With Android Studio 0.6.1+ (and possibly earlier) you can easily develop standard Java (non-Android) apps.
This method has been tested on 0.8.2:
Start by creating a vanilla Android Phone app, using File > New Project. Then add a Java Library module to hold your Java Application code. (Choose 'Java Library' even if you're building an application). You'll find you can build and run Java apps with main() methods, Swing apps etc.
You'll want to delete the auto-generated Android "app" module, which you're not using. Go to File -> Project Structure, and delete it (select the "app" module in the box on the left, and click the 'minus' icon above the box). Now when you reopen File -> Project Structure -> Project, you'll see options for selecting the project SDK and language level, plus a bunch of other options that were previously hidden. You can go ahead and delete the "app" module from the disk.
In 0.6.1 you could avoid creating the android module in the first place:
Go to File > New Project. Fill in your application name. On the "form factors" selection page, where you state your minimum Android SDK, deselect the Mobile checkbox, and proceed with creating your project.
Once the project is created, go to File -> Project Structure -> Project, and set your JDK as the "Project SDK". Add a Java Library module to hold your application code as above.
Here's exactly what the setup looks like.
Edit Configurations > '+' > Application:
I found a somewhat hacky, annoying and not-completely-sure-it-always-works solution to this. I wanted to share in case someone else finds it useful.
In Android Studio, you can right-click a class with a main method and select "Run .main()". This will create a new Run configuration for YourClass, although it won't quite work: it will be missing some classpath entries.
In order to fix the missing classpath entries, go into the Project Structure and manually add the output folder location for your module and any other module dependencies that you need, like so:
File -> Project Structure ...
Select "Modules" in the Project Settings panel on the left-column panel
Select your module on the list of modules in the middle-column panel
Select the "Dependencies" tab on the right-column panel
And then for the module where you have your Java application as well as for each of the module dependencies you need:
- Click "+" -> "Jars or directories" on the far right of the right-column panel
- Navigate to the output folder of the module (e.g.: my_module/build/classes/main/java) and click "OK"
- On the new entry to the Dependencies list, on the far right, change the select box from "Compile" to "Runtime"
After this, you should be able to execute the Run configuration you just created to run the simple Java application.
One thing to note is that, for my particular [quite involved] Android Studio project set-up, I have to manually build the project with gradle, from outside Android Studio in order to get my simple Java Application classes to build, before I run the application - I think this is because the Run configuration of type "Application" is not triggering the corresponding Gradle build.
Finally, this was done on Android Studio 0.4.0.
I hope others find it useful. I also hope Google comes around to supporting this functionality soon.
I installed IntelliJ IDEA community version from http://www.jetbrains.com/idea/download/
I tried opening my project that I started in Android studio but it failed when at gradle build. I instead opened both android studio and intellij at same time and placed one screen next to the other and simply drag and dropped my java files, xml layouts, drawables, and manifest into the project hiearchy of a new project started in IntelliJ. It worked around the gradle build issues and now I can start a new project in IntelliJ and design either an android app or a basic Java app. Thankfully this worked because I hated having so many IDEs on my pc.
I have been able to do it using following steps:
Open Android Studio and select 'Import Project'.
Browse to your project folder in the browse window and select it.
Tested in Android Studio 0.8.14:
I was able to get a standard project running with minimal steps in this way:
In an open Android Studio project, click File > New Module.
Click More Modules > Java Library > Next, then fill in whatever you prefer for the names.
A new module will appear as a folder on the same level as your "app" folder in the Project Structure. Open it and open the new Java class file.
You can then add your code, and choose Build > Run 'YourClassName'. Presto, your code is running with no Android device!
It works perfect if you do File>Open... and then select pom.xml file. Be sure to change the dropdown at the top-left of the sidebar that says "Android" to "Project" to see all your files. Also I think it helps if the folder your pom.xml file is in a folder called "app/".
Disclaimer: My java project was generated by Google App Engine.
Spent a day on finding the easiest way to do this. The purpose was to find the fastest way to achieve this goal. I couldn't make it as fast as running javac command from terminal or compiling from netbeans or sublime text 3. But still got a good speed with android studio.
This looks ruff and tuff way but since we don't initiate projects on daily bases that is why I am okay to do this.
I downloaded IntelliJ IDEA community version and created a simply java project. I added a main class and tested a run. Then simply closed IntelliJ IDEA and opened Android Studio and opened the same project there. Then I had to simply attach JDK where IDE helped me by showing a list of available JDKs and I selected 1.8 and then it compiled well. I can now open any main file and press Control+Shift+R to run that main file.
Then I copied all my Java files into src folder by Mac OS Finder. And I am able to compile anything I want to.
There is nothing related to Gradle or Android and compile speed is pretty good.
Thanks buddies
To run a java file in Android ensure your class has the main method. In Android Studio 3.5 just right click inside the file and select "Run 'Filename.main()'" or click on "Run" on the menu and select "Run Filename" from the resulting drop-down menu.
on Android Studio 4.0 and above, you will get an option readily on the IDE,a green run icon to run the related main() class.

Resources