I have two folders:
Network/
+ Network.java
Router/
+ Router.java
Is there any way in java to import Network.java into Router/ folder? Or I can only copy it directly into a folder for this to work?
You can import things from nearby folders using packages.
For instance, if you have this file structure (in which I have changed the names of the folders to lower case):
network/Network.java
router/Router.java
And you have
package network;
at the top of Network.java, and
package router;
at the top of Router.java, then you can import Network into Router.java using:
import network.Network;
This assumes that you are trying to import a public class, declared inside Network.java as:
public class Network {
...
IDE:
If you're using an IDE like Netbeans oder Eclipse, you can just drag and drop the file to the package required. After this action the IDE will perform some refactoring and that's it. For more details refer to the posting above.
Related
Good morning everyone!
I did a project x but all my files were out of order so I decided to group them by folders.
foldera
---ClassA.java
---ClassB.java
folderb
---Class1.java
Main.java
The problem arises when I try to compile, since in the main it appears that the classes I made are not found
I thought this could be solved by putting in the classes
package src.foldera.ClassA;
And in the others the same
package src.foldera.ClassB;
And
package src.folderb.Class1;
So in all classes
And in the main put
import src.foldera.*;
import src.folderb.*;
But I keep getting the same error even though I put the packages
It should be noted that I did not create the folders in the code editor, rather I did it in the same Windows 10 File system
What is this about? Thanks!
Assuming a standard setup where the src folder is the root of your source hierarchy, the statements should be:
package foldera;
package folderb;
The imports should be similarly shortened to:
import foldera.*;
import folderb.*;
You have to define the package in which the class is in.
As an example in classA:
package src.foldera;
And in class1:
package src.folderb;
import src.foldera.ClassA;
I'm trying to work off of an existing code base, and one of the files has an import statement accessing the path game.format.noFlash.*. However the format folder only has files for the other classes; the noFlash file folder is nowhere to be found.
I know it must be there because when I begin to type in the import statement in intelliJ, one of the autofill options is the noFlash folder. Is there a way to access it, and if so how?
The auto-fill options are display packages and also class names
I suspect the noFlash is a class and not a folder and Intellij is suggestion that you can import it, for example:
Classes can be specified explicitly on import instead of using the wildcard character.
import javax.swing.JOptionPane; // Make a single class visible.
I want to use the StdDraw package, and I've tried many different ways of importing it.
Of course I tried:
import StdDraw;
But, when I look at the documentation from Princeton here, it shows that StdDraw is part of Objects, so I try this:
import java.lang.Object.StdDraw;
However, this results in an error:
error: cannot find symbol in
import java.lang.Object.StdDraw;
I saw this question here but it does not answer this question either.
How do I import StdDraw? thank you.
if you want to use StdDraw you must have
either the sources
or the classes (best zipped up as jar)
as preferred way you use the sources (see http://introcs.cs.princeton.edu/java/15inout/). it says there "To use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java into the same directory as your program. "
once you did this the imports should be working.
NOTE: all four files are not in packages, so you should 'download' them into the 'standard' package. That means you have to download them to the root package of your project.
by the way: don't import import java.lang.Object.StdDraw; but do just import import StdDraw;
First of all check encoding of your IDE. It should be set to UTF-8. It is important if you are using MS Windows operating system.
Then create StdDraw.java class in the same package as the package of your program you are writing. Remove class declaration, leave only package declaration.
Then visit this page: https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java .
Copy all its contents (Ctr-A, Ctrl-C) and then paste it into StdDraw.java file you created previously.
StdDraw.java has its own main method so try to run it in order to check that the library works correctly. You should see a window with four strange figures :) .
Don't touch StdDraw.java anymore. Now you can easily import StdDraw library and refer to its methods with name of the class.
Enjoy
I imported an external JAR to my project in Eclipse, by following these instructions:
Right click on the project > Build Path > Add external archives > Choosing the JAR file from the hard drive.
The JAR file than appears in the 'References libraries' section in my project, and works fine.
However, I never need to use the import keyword in my classes in order to use the classes from the JAR. I find this weird, I thought I'd have to use import myImportedJar or something similar for this to work.
Is it normal that I don't have to use the import keyword? Did I do something wrong?
The import statement is used to be able to refer to types and their members by their simple names. You don't need to import classes that are in the same package, unless they are nested members of other classes. This is true regardless of where the class comes from, for example, if it's in another .jar.
The import keyword works on a package level. If these packages are supplied by jars or not is not a feature of the java programming language.
import only makes a name available in unqualified form in your program. The following code fragments are identical:
java.util.List<Object> list = new java.util.ArrayList<>()
vs
import java.util.List;
import java.util.ArrayList;
List<Object> list = new ArrayList<>()
Now, importing a jar file in eclipse puts the classes from this jarfile on the classpath - the total "world" of available classes for your application.
I'm a seasoned PHP programmer with backgrounds within Powershell, JavaScript etc.
I see the benefits with Java and wish to dig deeper.
All the documentation though is too advanced for me atm.
A typical PHP structure:
index.php
classes/class_get.php
classes/class_set.php
Where in index.php you would have the lines
require_once "classes/class_get.php";
require_once "classes/class_set.php";
Now in Java...
I understand Windows files are in .JAR format. No problem here, easy to understand.
I am confused when creating this in eclipse. Is the strucutre;
Main.java
src*/defaultPackage**/myClass.java
* folder
** package
Where in the Main.java file you would declare "import myClass.java"?
Also other packages such as the following:
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
I am getting errors, and this manifest file, I haven't heard of it in any tutorials yet?
Try this, this is the way to create a jar or runnable jar in eclipse
File -> Export-> Java ->Runnbale JAR file
Launch configuration : your Class containing the public static void main(String[] args)
Export destination : Target place
Library Handling:
Package required libraries into generated JAR
FINISH
a) There are no Windows files in Java. Java is cross platform.
b) Something with a slash delimiter is always a folder. Something with a dot is always a package. Don't confuse them, because it is confusing enough.
c) Don't use the term "defaulPackage", because there is such a term for the case, that you don't specify any package. Then the package of your class is called the default package.
Main.java
src*/defaultPackage**/myClass.java
Where in the Main.java file you would declare "import myClass.java"?
You never import something .java, because you import a class, not a source file. Often you only have third party compiled classes in a jar, and don't have the source. Well - maybe.
If your class belongs to a package, the name of the class is the whole package name. You can omit it from classes in the same package.
So we don't know whether Main and MyClass (use Upper case, if you like to communicate with others - else you're confusing us) belong to the same package.
If so: Don't import anything.
Else: Import the whole package name, which might contain multiple dots.
So for example:
import yourCompany.games.monstersahead.*;
or
import yourCompany.games.monstersahead.MyClass;
for example.
The package name will usually not contain folder names like src, bin, classes.