I generate (from a progam java) some Java classes but actually, I don't add the import in each class. I'd like to have a kind of parser that is able to see which import needs to be added in a class. Maybe, it's possible to reuse the fonctionnality of Eclipse because it's able to do that. However, I don't know where I can find that.
Do you have any idea?
Many thanks,
Bat
What about using only Full Qualified Names for classes when creating the code for the generated Java classes?
What are you going to do about name collisions? You use a "Date" - is it java.util.Date or java.sql.Date? You use a "XMLFoo", is it com.foomatics.XMLFoo or is it org.openfoo.XMLFoo? It's better to add the imports as you add the code.
Maybe you can create a HashSet of all the imports you're going to need as you generate the code, and then add them in at the top when you're done?
I see now... the problem is definitely on your generator. Work on that because even if you can extract the functionality from Eclipse, you won't get a 100% working solution every time. So my suggestion is just forget it and fix your generator.
As Paul said, you have to decide the imported class yourself. I usually type on Eclipse and at the end of the class I press Ctrl+Space to get some suggestions. So, for example I want to have java.util.Date, I will write Date and then Ctrl+Space and select java.util.Date and Eclipse will automatically import java.util.Date for me.
If the code is already there, I will do quick fix (F1 on Eclipse) and it will also suggest some fixes to the code I have (one of them is to import the suitable class).
Related
I am trying to write a standalone Java application in IntelliJ using edu.stanford.nlp.trees.GrammaticalStructure. Therefore, I have imported the module:
import edu.stanford.nlp.trees.GrammaticalStructure;
Currently, Intellij doesn't recognize this and many others of the imported external libraries (cannot resolve the symbols) and is also not able to automatically download/import them.
Is there a way to use the GrammaticalStructure class without having to download the entire Stanford CoreNLP .jar and adding it to the project as a library? This question applies to other dependencies as well, since I want to use other external libraries but avoid including their .jar files as much as possible (to minimize the size of the final application, given that it will be standalone). Unfortunately, all the solutions I have found proposed exactly that.
Apologies if I have overlooked some basic setting or setup steps, it has been a while since I have worked with Java.
Any help is greatly appreciated.
If you want to use it means you want to execute the code in them. How is the runtime supposed to execute code that is does not have? How is the compiler supposed to know how the code is defined (e.g. what the classes look like)? This is simply impossible. If you want to use the code you have to provide it to the compiler as well as the runtime.
If you just dont want to include all of that code into your application, you need either access to the sources and just pick the class you need or you need some kind of JAR minimizer as #CrazyCoder suggested.
As you can read in the title "Optimize imports on the fly" does not work for me in Kotlin, but it works in Java, i. e. IDEA removes unused imports in my Java files but not in my Kotlin files.
Since I didn't find any posts related to this on the internet I was wondering if this bug is only occurring for me.
Maybe there is a good reason for this, if so feel free to tell me.
Works partly for me. That means when editing or saving a file unused imports are not cleaned. However, when I paste code, imports are automatically inserted.
I personally always use "Optimize imports".
This error does no occur anymore. I am not sure which version of IDEA/Kotlin plugin it was fixed with, but I guess since August this year this should work.
Though a Java novice, I managed to import plain text from the clipboard (sourced in Excel). It looks like the ClipboardContent class would allow me to import formatted text (html, xml...) but my attempts so far always result in "class not found" for any reference to ClipboardContent.
I suspect there's an import (or full reference) that will fix this but my references and on-line searches have not been fruitful so far.
I'll appreciate any clues I can get.
Java has some pretty great documentation online.
here is where you can find anything about that you could need that has to do with basic java's classes. I recommend going here using a ctrl+F for whatever class you need and click on it. on that page it will give you everything you need to know about it.
javafx clipboardcontent api
Make sure you import "javafx.scene.input.ClipboardContent" (assuming this is what you need).
Just a FYI - you can run into cases were you find similar or identical named classes in other packages. If you are using Eclipse IDE, you can hover over an class object and it will provide you a hint on where to import it from if Eclipse has those dependencies. If it doesn't have it, then you will need to add that dependency (like a jar file) and add it as part of your library.
I am a programming enthusiast with a basic programming background, but I'm completely new to the Java programming language.
I want to learn how a simple web crawler is built and I'm using this site to compile the source to see how it works and see it in action!
http://java.sun.com/developer/technicalArticles/ThirdParty/WebCrawler/#demo
The source provided by the website is here:
http://java.sun.com/developer/technicalArticles/ThirdParty/WebCrawler/WebCrawler.java
I am running eclipse 3.2 and using the sun-java-6 JRE to compile applets. I am running on Crunchbang, a Ubuntu distro.
There is some part of the library that I am unfamiliar with and do not know how to fix.
List listmatches;
The error says that "The type List is ambiguous".
I have the package java.utils.*; but the error still persist.
Is there something wrong with my syntax or is there a new syntax for List?
Add import java.awt.List; to your import statements. This should work fine then.
This is mainly because there is a java.util.List and a java.awt.List. Since you are importing both of them using wildcards, the compiler doesn't know which one you actually want to.
The reason that you get the "ambiguous" message is because there is a List class in both "java.awt." and the "java.util." packages that are at the top of the import list.
To solve this issue you should pick one of them that most likely is being used in the application (I would guess java.awt.List.
In eclipse if you do a "CTRL + SHIFT + o" (that's an o not a zero), it will refactor your imports. From there you can select the java.awt.List.
To avoid such ambiguous message, we have to import either "java.util.List" or "java.awt.List" specifically based on your need. If you use both, then the confusion may arise such problem.
To see example program using this, please visit http://www.cjavaprogramsprojects.com/java-final-year-mini-project-file-loader
I am currently on a task, where I need to fix the deprecated items in the existing projects. I mean I have to replace the deprecated items with the corresponding replacing items as given in javadocs,
Eg: java.util.Date.setSeconds(int): Instead of this deprecated method, we need to use Calendar.set(Calendar.SECOND, int seconds).
I need to automate this using java code in eclipse IDE, by giving a project name in the workspace as input.
Kindly suggest me in doing this.
Thanks in advance.
I would go with the search & replace functionality of your IDE, by utilizing regex. (your parameter values should be captured with regex)
There isn't any specific utility to replace deprecated code, because it is not always that case that there is a straightforward replacement. Sometimes there is not replacement, and in other cases there is a completely different approach.
If the project is not really big probably easiest way is to do this by hand. It also handles the situations where there is no direct replacement.
Alternative (and definitely more interesting) way would be to write an eclipse plugin that extends eclipses JDT. Also if you are on Java 6 one possibility is to use Java compiler API.