Intellij idea auto import across files - java

I have auto import enabled in idea, but it requires me to open the file in the editor (like it should). Now, i have done some regex magic, which means across 100+ classes i am using new classes that need to be imported. Since its all done with find/replace, those files have never been opened in the editor, and therefore the new classes havent been auto imported. Is there any way to run auto import unambiguous references across all files? cause currently, i have to compile, and then open all the files from the errors window? Optimize imports aparently doesnt do new imports.

I am not sure in which version of IntelliJ this feature became available (I am currently using 2017.1.1) but you can select a package from the project browser and in the context menu optimise imports for that package or use the keyboard shortcut Ctrl+Alt+o.
I just used this to update the imports of all my classes after changing my import settings and it worked like charm.

Related

Eclipse type / import error with built java file

I have a project where we use a ant buildfile to create some .java files. Those built files need to reference to already existing other java files.
My problem is that the built file can have a code line as for example
arg = (Expr) new Ast.BinaryOP(lhs, BOp.B_MINUS, rhs);
where it will show an error "Ast.BinaryOP cannot be resolved to a type". I then can type Ast.BinaryOP again and use autocomplete to pick the BinaryOP part and the error will disappear. The same with BOp.B_MINUS.
I did import (probably way too many times) the necessary other files as far as I can tell.
I.e.
import cd.ir.Ast;
import cd.ir.Ast.*;
import cd.ir.Ast.Expr;
import cd.ir.Ast.BinaryOp;
import cd.ir.Ast.BinaryOp.BOp;
So far I tried
restarting Eclipse
'clean' the project
to just run the project anyway (doesn't work)
remove the "Ast." part which will work once again if I do it in Eclipse but not if generated that way
Google the problem, respectively search it on Stackoverflow which turned out to be hard as there are many related issues. There are no good keywords as far as I can tell.
deleted the project from Eclipse and imported it newly
I need to repeatedly run the ant build file to test the code from where the .java file is generated, thus changing all errors manually is not an option.
Thanks for any advice/help in advance. I will gladly provide more information if you tell me what could help.
Hit F5 on the project, open the file, and then try hitting Ctrl+Shift+O to automatically organize the imports.
After consulting with a friend who didn't see the issue either I finally found it. So logical in retrospect but small enough that I spend 2,5h trying to find it:
import cd.ir.Ast.BinaryOp;
vs:
new Ast.BinaryOP(....
BinaryOp vs. BinaryOP

Again, R.java not found

I have a lot of java files in my project so I decided to create packages and organize those files. After putting them in seperate packages like
com.myproject.android.activity
com.myproject.android.adapter
etc...
eclipse wants me to import the R file. From different SO questions I know "Never import the R file". However without that eclipse shows error messages that R.java is missing and wants to import it.
I already did a eclipse restart, clean, and android->fix with no success. My resource files are without errors. Is it safe to import the R file. Any suggestions?
Yes, you can import R files, they are sometimes needed to be imported, e.g. when you define them in a library and you need to use them in the package that uses that library.
You can also use full names - this may make the code more clear even if longer.

Creating a Java Library or class or package with Jar files

I have about 100 jar files and I think I want to make a library with them. What is the best way to do this? How does importing work with this. Do I still have to ask for each class or do I just refer to the new library?
More detail
I have the GeoTools9.4 package (in a zip). It has about 100 jar files. When I import these into my project in eclipse, it takes each jar file and stuffs it in and clutters up my structure. So I think I want a library (or a package or a class) I am not sure what the terminology is here.
More detail on how to call the classes in the new library.
Right now here is how I call the classes
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
If I put all of these jar files in a library can I replace the above lines with a new import like
import org.geotools.local
or do I not need to change the way they are called?
I propose you to use a Maven for this stuff.
Maven is a greay build tool, that could take care of problems, like adding dependency jars to a project. Also, GeoTools support Maven and have a nice manual for it (http://docs.geotools.org/latest/userguide/tutorial/quickstart/maven.html)
About last question - when you'll add this libraries, full name of these classes will be the same, so you must import and use them as you import them right now.
This is not usually refered to as "calling" the classes, but rather importing the classes meaning that they become available to the class that uses them.
No matter how you have those classes (in many jars or a single jar) you still need to have the import statements in the beggining of the class file for the source to be compiled
I am not sure how Eclipse "clutters up" your structure. You can place all the jars in a single folder e.g. lib and then import them into your eclipse project from that folder. If you mean that the jars show up in the left pane then there are filters that can hide them. In Eclipse there is the concept of a Working set where you can select what it would be visible and what not.
I hope it helps

Java class methods do not auto complete in Jython using Eclipse Pydev

I'm using PyDev 2.5 with Eclipse Indigo and Jython 2.5.3b1 . I have a JAR file that contains certain classes which I'm importing to a PyDev (Jython ) project. They seem to work seamlessly except for Auto completion. The member functions of Java Classes do not auto-complete e.g. pressing the dot '.' operator does not bring up the list for class member functions. The jar file is added to the PyDev-PYTHONPATH external libraries of the PyDev project.
Screenshot of PYTHONPATH external libs
Auto completion does not work for the code below, but it compiles and runs perfectly fine.
from my.testpackage import MyClass
myVar = MyClass("Monkey")
print myVar.getName()
Typing "myVar." does not auto complete
Worth noting that auto completion works if I imported a non custom jar
e.g.
from java.lang import Math
print Math.max(3,5)
Typing "Math." will auto complete
I'm not sure if this functionality even supported in the current version of PyDev. Does anyone actually have this working in their PyDev and Eclipse setup?
Any suggestion would be appreciated.
Thank you,
DM
It may be some issue in your PYTHONPATH configuration. Have you read: http://pydev.org/manual_101_project_conf2.html (most specifically the end of the page: "Project reference for Jython users").
If that doesn't help you, can you explain how are you referencing things? (a screenshot with the config would be nice)

Import Error in Java Using Ant but not Eclipse

I can compile my program just fine if I specify sublibraries of com.itextpdf.*, but for some reason, my compile fails if I simply do import com.itextpdf.*. But, like I said, if I specify the sublibraries I'm using, such as
import com.itextpdf.text.Font.*;
import com.itextpdf.text.pdf.*;
Everything compiles just fine. In the case where it fails, the error I'm getting is "package com.itextpdf does not exist", but I know it does. I include it properly in the build path, and Eclipse doesn't give any compile errors in any case. The error only comes up when I try to build with Ant.
At this point, I don't mind specifying the sub libraries I'm going to use to make sure I get a build. But I am curious, why wouldn't Ant allow this?
You, and Eclipse, think of java namespaces as a hierarchy. However, in the Java specification, they are not. There is no relationship between "com.itextpdf.text" and "com.itextpdf.text.Font". They are different strings, end of story.
As a matter of style, you may want to set up Eclipse to automatically organize your imports and explicitly specify every class you use in the import section of your class. This avoids ambiguity when "com.itextpdf.text.Font.String" and "com.itextpdf.text.pdf.String" both exist, but you only want to reference one of them. This style would be incredibly tedious and inconvenient if you were programming in emacs or vi, but with Eclipse it is automatic and invisible, unless you scroll to the top of your file.

Categories