For example, the third party library uses very complex directory structure for the package.
Can I import them at once. the star seems can only imports one directory level.
Assuming you're asking whether or not you can do something like:
import com.example.*;
// expecting packages like com.example.foo.* and com.example.bar.* to be imported
Then the answer is no. The widest import you can make is a single package with a star.
The JLS (section 7.5) defines the structure for import statements.
Additionally, it would be problematic for the compiler to import a bunch of packages at once. If two packages provide the same type, both are imported, and the type is used in the class, a compile-time error occurs. This is described in JLS 6.5.5.1:
Otherwise, if a type of that name is
declared by more than one
type-import-on-demand declaration of
the compilation unit, then the name is
ambiguous as a type name; a
compile-time error occurs.
As you pointed in java you can only use "one level" import used by "*"
The more effective way is to open Eclipse, open class where imports should be and press: CTRL + SHIFT + O:) - nowadays all IDE helps with organize imports
I strongly suggest, as others have done, that you use the "organize imports" from your IDE (most major java IDE will do this).
Also, I advice against using "*" in your imports, and the reason for this is simple. At some time, you might want to upgrade one of the library that you import (or the jdk) and you might end up with name clashes due to some new classes in a package (this is especially true for the static imports). It might not be a big deal if that happens to a class inside your current project, but if you make that code into a library at some point, it might be more problematic as you will have to recompile that class after adjusting the conflicting imports.
(Yes, that happened to me not so long ago... It was a pain as I had to hunt down the library source code to rebuild.)
Save yourself some trouble down the road. :D
As others have already said, you can't import all packages at once. However, its not like the end of the world. If you use a good IDE like Intellij, it would add import statements with appropriate package name automatically to your code as soon as you write the code which is using a class/interface from that package.
There is another way if you want to import everything at once. This is not at all recommended but the trick is to go to https://docs.oracle.com/en/java/javase/16/docs/api/allpackages-index.html to check the list of all packages, save that html page on your local system, open the html code. remove all tags from head tag and add this code:
<style>
.block { display: none;}
</style>
After this, save the document and open it in a browser, you'll see the package names clearly without any description. Now, you can just copy-paste them and give them as input to a simple java program that converts them into proper import statements.
The final result is:
import com.sun.jarsigner.*;
import com.sun.java.accessibility.util.*;
import com.sun.jdi.*;
import com.sun.jdi.connect.*;
import com.sun.jdi.connect.spi.*;
import com.sun.jdi.event.*;
import com.sun.jdi.request.*;
import com.sun.management.*;
import com.sun.net.httpserver.*;
import com.sun.net.httpserver.spi.*;
import com.sun.nio.sctp.*;
import com.sun.security.auth.*;
import com.sun.security.auth.callback.*;
import com.sun.security.auth.login.*;
import com.sun.security.auth.module.*;
import com.sun.security.jgss.*;
import com.sun.source.doctree.*;
import com.sun.source.tree.*;
import com.sun.source.util.*;
import com.sun.tools.attach.*;
import com.sun.tools.attach.spi.*;
import com.sun.tools.javac.*;
import com.sun.tools.jconsole.*;
import java.applet.*;
import java.awt.*;
import java.awt.color.*;
import java.awt.datatransfer.*;
import java.awt.desktop.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.im.*;
import java.awt.im.spi.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import java.awt.print.*;
import java.beans.*;
import java.beans.beancontext.*;
import java.io.*;
import java.lang.*;
import java.lang.annotation.*;
import java.lang.constant.*;
import java.lang.instrument.*;
import java.lang.invoke.*;
import java.lang.management.*;
import java.lang.module.*;
import java.lang.ref.*;
import java.lang.reflect.*;
import java.math.*;
import java.net.*;
import java.net.http.*;
import java.net.spi.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.nio.charset.spi.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.spi.*;
import java.rmi.*;
import java.rmi.activation.*;
import java.rmi.dgc.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.security.*;
import java.security.cert.*;
import java.security.interfaces.*;
import java.security.spec.*;
import java.sql.*;
import java.text.*;
import java.text.spi.*;
import java.time.*;
import java.time.chrono.*;
import java.time.format.*;
import java.time.temporal.*;
import java.time.zone.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import java.util.jar.*;
import java.util.logging.*;
import java.util.prefs.*;
import java.util.regex.*;
import java.util.spi.*;
import java.util.stream.*;
import java.util.zip.*;
import javax.accessibility.*;
import javax.annotation.processing.*;
import javax.crypto.*;
import javax.crypto.interfaces.*;
import javax.crypto.spec.*;
import javax.imageio.*;
import javax.imageio.event.*;
import javax.imageio.metadata.*;
import javax.imageio.plugins.bmp.*;
import javax.imageio.plugins.jpeg.*;
import javax.imageio.plugins.tiff.*;
import javax.imageio.spi.*;
import javax.imageio.stream.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.*;
import javax.management.*;
import javax.management.loading.*;
import javax.management.modelmbean.*;
import javax.management.monitor.*;
import javax.management.openmbean.*;
import javax.management.relation.*;
import javax.management.remote.*;
import javax.management.remote.rmi.*;
import javax.management.timer.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.event.*;
import javax.naming.ldap.*;
import javax.naming.ldap.spi.*;
import javax.naming.spi.*;
import javax.net.*;
import javax.net.ssl.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;
import javax.rmi.ssl.*;
import javax.script.*;
import javax.security.auth.*;
import javax.security.auth.callback.*;
import javax.security.auth.kerberos.*;
import javax.security.auth.login.*;
import javax.security.auth.spi.*;
import javax.security.auth.x500.*;
import javax.security.cert.*;
import javax.security.sasl.*;
import javax.sound.midi.*;
import javax.sound.midi.spi.*;
import javax.sound.sampled.*;
import javax.sound.sampled.spi.*;
import javax.sql.*;
import javax.sql.rowset.*;
import javax.sql.rowset.serial.*;
import javax.sql.rowset.spi.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
import javax.swing.plaf.multi.*;
import javax.swing.plaf.nimbus.*;
import javax.swing.plaf.synth.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import javax.swing.text.rtf.*;
import javax.swing.tree.*;
import javax.swing.undo.*;
import javax.tools.*;
import javax.transaction.xa.*;
import javax.xml.*;
import javax.xml.catalog.*;
import javax.xml.crypto.*;
import javax.xml.crypto.dom.*;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dsig.dom.*;
import javax.xml.crypto.dsig.keyinfo.*;
import javax.xml.crypto.dsig.spec.*;
import javax.xml.datatype.*;
import javax.xml.namespace.*;
import javax.xml.parsers.*;
import javax.xml.stream.*;
import javax.xml.stream.events.*;
import javax.xml.stream.util.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stax.*;
import javax.xml.transform.stream.*;
import javax.xml.validation.*;
import javax.xml.xpath.*;
import jdk.dynalink.*;
import jdk.dynalink.beans.*;
import jdk.dynalink.linker.*;
import jdk.dynalink.linker.support.*;
import jdk.dynalink.support.*;
import jdk.javadoc.doclet.*;
import jdk.jfr.*;
import jdk.jfr.consumer.*;
import jdk.jshell.*;
import jdk.jshell.execution.*;
import jdk.jshell.spi.*;
import jdk.jshell.tool.*;
import jdk.management.jfr.*;
import jdk.net.*;
import jdk.nio.*;
import jdk.security.jarsigner.*;
import netscape.javascript.*;
import org.ietf.jgss.*;
import org.w3c.dom.*;
import org.w3c.dom.bootstrap.*;
import org.w3c.dom.css.*;
import org.w3c.dom.events.*;
import org.w3c.dom.html.*;
import org.w3c.dom.ls.*;
import org.w3c.dom.ranges.*;
import org.w3c.dom.stylesheets.*;
import org.w3c.dom.traversal.*;
import org.w3c.dom.views.*;
import org.w3c.dom.xpath.*;
import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;
You can just copy-paste this to your .java files
Here's the java code that converts the name of packages to import statements:
import java.util.Scanner;
public class import_everything {
public static void main(String[] args) {
String[] s = new String[233];
int i = 0;
Scanner sc = new Scanner(System.in);
while (i < 223) {
s[i] = sc.next();
s[i] = "import " + s[i] + ".*;";
i++;
}
i = 0;
while (i < 223) {
System.out.println(s[i]);
i++;
}
}
}
And here's the list of all the java packages obtained by changing html code of the official documentation provided at https://docs.oracle.com/en/java/javase/16/docs/api/allpackages-index.html :
com.sun.jarsigner
com.sun.java.accessibility.util
com.sun.jdi
com.sun.jdi.connect
com.sun.jdi.connect.spi
com.sun.jdi.event
com.sun.jdi.request
com.sun.management
com.sun.net.httpserver
com.sun.net.httpserver.spi
com.sun.nio.sctp
com.sun.security.auth
com.sun.security.auth.callback
com.sun.security.auth.login
com.sun.security.auth.module
com.sun.security.jgss
com.sun.source.doctree
com.sun.source.tree
com.sun.source.util
com.sun.tools.attach
com.sun.tools.attach.spi
com.sun.tools.javac
com.sun.tools.jconsole
java.applet
java.awt
java.awt.color
java.awt.datatransfer
java.awt.desktop
java.awt.dnd
java.awt.event
java.awt.font
java.awt.geom
java.awt.im
java.awt.im.spi
java.awt.image
java.awt.image.renderable
java.awt.print
java.beans
java.beans.beancontext
java.io
java.lang
java.lang.annotation
java.lang.constant
java.lang.instrument
java.lang.invoke
java.lang.management
java.lang.module
java.lang.ref
java.lang.reflect
java.math
java.net
java.net.http
java.net.spi
java.nio
java.nio.channels
java.nio.channels.spi
java.nio.charset
java.nio.charset.spi
java.nio.file
java.nio.file.attribute
java.nio.file.spi
java.rmi
java.rmi.activation
java.rmi.dgc
java.rmi.registry
java.rmi.server
java.security
java.security.acl
java.security.cert
java.security.interfaces
java.security.spec
java.sql
java.text
java.text.spi
java.time
java.time.chrono
java.time.format
java.time.temporal
java.time.zone
java.util
java.util.concurrent
java.util.concurrent.atomic
java.util.concurrent.locks
java.util.function
java.util.jar
java.util.logging
java.util.prefs
java.util.regex
java.util.spi
java.util.stream
java.util.zip
javax.accessibility
javax.annotation.processing
javax.crypto
javax.crypto.interfaces
javax.crypto.spec
javax.imageio
javax.imageio.event
javax.imageio.metadata
javax.imageio.plugins.bmp
javax.imageio.plugins.jpeg
javax.imageio.plugins.tiff
javax.imageio.spi
javax.imageio.stream
javax.lang.model
javax.lang.model.element
javax.lang.model.type
javax.lang.model.util
javax.management
javax.management.loading
javax.management.modelmbean
javax.management.monitor
javax.management.openmbean
javax.management.relation
javax.management.remote
javax.management.remote.rmi
javax.management.timer
javax.naming
javax.naming.directory
javax.naming.event
javax.naming.ldap
javax.naming.ldap.spi
javax.naming.spi
javax.net
javax.net.ssl
javax.print
javax.print.attribute
javax.print.attribute.standard
javax.print.event
javax.rmi.ssl
javax.script
javax.security.auth
javax.security.auth.callback
javax.security.auth.kerberos
javax.security.auth.login
javax.security.auth.spi
javax.security.auth.x500
javax.security.cert
javax.security.sasl
javax.smartcardio
javax.sound.midi
javax.sound.midi.spi
javax.sound.sampled
javax.sound.sampled.spi
javax.sql
javax.sql.rowset
javax.sql.rowset.serial
javax.sql.rowset.spi
javax.swing
javax.swing.border
javax.swing.colorchooser
javax.swing.event
javax.swing.filechooser
javax.swing.plaf
javax.swing.plaf.basic
javax.swing.plaf.metal
javax.swing.plaf.multi
javax.swing.plaf.nimbus
javax.swing.plaf.synth
javax.swing.table
javax.swing.text
javax.swing.text.html
javax.swing.text.html.parser
javax.swing.text.rtf
javax.swing.tree
javax.swing.undo
javax.tools
javax.transaction.xa
javax.xml
javax.xml.catalog
javax.xml.crypto
javax.xml.crypto.dom
javax.xml.crypto.dsig
javax.xml.crypto.dsig.dom
javax.xml.crypto.dsig.keyinfo
javax.xml.crypto.dsig.spec
javax.xml.datatype
javax.xml.namespace
javax.xml.parsers
javax.xml.stream
javax.xml.stream.events
javax.xml.stream.util
javax.xml.transform
javax.xml.transform.dom
javax.xml.transform.sax
javax.xml.transform.stax
javax.xml.transform.stream
javax.xml.validation
javax.xml.xpath
jdk.dynalink
jdk.dynalink.beans
jdk.dynalink.linker
jdk.dynalink.linker.support
jdk.dynalink.support
jdk.javadoc.doclet
jdk.jfr
jdk.jfr.consumer
jdk.jshell
jdk.jshell.execution
jdk.jshell.spi
jdk.jshell.tool
jdk.management.jfr
jdk.nashorn.api.scripting
jdk.nashorn.api.tree
jdk.net
jdk.nio
jdk.security.jarsigner
netscape.javascript
org.ietf.jgss
org.w3c.dom
org.w3c.dom.bootstrap
org.w3c.dom.css
org.w3c.dom.events
org.w3c.dom.html
org.w3c.dom.ls
org.w3c.dom.ranges
org.w3c.dom.stylesheets
org.w3c.dom.traversal
org.w3c.dom.views
org.w3c.dom.xpath
org.xml.sax
org.xml.sax.ext
org.xml.sax.helpers
just use somthing like
Import java.*
As In the case of JSPs,can I use a .inc file to import all the required classes from 3rd party libraries in a java class
You can import only one directory level.
In IntelliJ Ctrl + Alt + O shortcut can be helpful to:
organize existing imports
remove unneeded imports
add required imports
You can also adjust the auto-import settings under "Settings → Editor → General → Auto Import"
For Basic Usage Only You can use:
import java.*;
import javax.*;
* is for all
java.*;
all classes in java package.
Related
I'm running into an issue with Java compile error on OSX but all works fine on Windows 7.
On both platforms I'm using IntelliJ with Java 1.70 u80 SDK and Maven 3.3.9 (bundle from IntelliJ)
The error which appears on OSX is
incompatible types: Iterator cannot be converted to Iterator
for the code line which looks like
Iterator<Node> it = sh.extractAllHeaderElements();
while (it.hasNext())
{
...
}
Any clue why it works on Windows machine but not on Mac?
Edit:
Imports are:
import java.io.*;
import java.lang.management.ManagementFactory;
import java.net.URI;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletRequest;
import javax.xml.bind.Element;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nhindirect.xd.soap.type.MetadataLevelEnum;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
The problem appears to be in the Iterator that is being returned with sh.extractAllHeaderElements(); Instead of returning a Iterator<Node> it is returning a Iterator of some other type when run on the mac platform. Thus useing a wildcard with the Iterator interface will work.
To do this just replace the Iterator<Node> it = sh.extractAllHeaderElements(); with Iterator<?> it = sh.extractAllHeaderElements();
I use NetBeans IDE to do a project in JavaFX. The IDE imports all the necessary import statements automatically. It do this specifically, that is only the necessary import statements are imported. The following is the list of import statements in one of my java application -
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
As seen, its a long list of import statements. One problem I found with this is that it takes more lines of code. Why don't they use the import statements like the following ?
import java.sql.*;
import java.util.logging.*;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Stage;
This saves a lot of lines of code.
What is the actual difference between the 2 sets of import statements ? Why NetBeans use import statements specifically ?
Let me demonstrate one of the worst problems with wildcard imports:
import org.example.mylib.*;
import java.util.*;
class X {
List<String> xs;
}
In version 2.0 of your mylib you add a class named List<T>. You don't touch the source code at all, and the source code has nothing to do with that added class.
If your added List defines all the methods which are used by class X, the code will build successfully, but the runtime behavior will change for deeply mysterious reasons.
There is the option to configure star imports in NetBeans. From the menu, select Tools -> Options. Click the Editor button on the top, then the formatting tab. From the Language dropdown select Java, and from the Category dropdown select Imports. Now you see several options to declare imports: Single Class, Package Imports, or no imports but use fully qualified names in the source. Single Class can be specified further, for example, the number of imports from a package before using a star import.
How do i get the classes from the import list, or where do i find the imported class files. for example
import game.sprites.PlayerSprite;
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.Vector;
import litecom.Trace;
import litecom.gfxe.LoaderTarget2;
import litecom.gfxe.Timer;
import litecom.scoreclient2.ScoreClient2;
any idea how to get those java. files?
You don't. The import statements are not preserved in the bytecode. When you compile Java sources to class files (bytecode), the compiler inserts the fully qualified name of the classes which are referenced.
Hi I have source code from a program ,and when I test in NetBeans there is and error in import:
java.util.Dict;
Can anybody help me, please?
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
import java.util.Dict;
There isn't a class called Dict. There is however a Dictionary class. Please check the API docs.
I have the participant in an RTP Session, I need to get the IP address from that Participant.
How do I do that?
My imports, by request of one of the answerers:
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.DataSink;
import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaException;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.Processor;
import javax.media.RealizeCompleteEvent;
import javax.media.format.AudioFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.media.rtp.Participant;
import javax.media.rtp.ReceiveStream;
import javax.media.rtp.ReceiveStreamListener;
import javax.media.rtp.SessionAddress;
import javax.media.rtp.SessionListener;
import javax.media.rtp.SessionManager;
import javax.media.rtp.event.NewParticipantEvent;
import javax.media.rtp.event.NewReceiveStreamEvent;
import javax.media.rtp.event.ReceiveStreamEvent;
import javax.media.rtp.event.SessionEvent;
import javax.media.rtp.rtcp.SourceDescription;
when a client connects to RTP server you will get a Socket and can use this method socket.getRemoteSocketAddress().toString(); to find the client IP address. For more detail post sample of your code.