I'm not quite sure what I'm doing wrong, here. I have two files in a directory, let's call them FileA.java and FileB.java.
FileA.java has a definition along the lines of:
package com.domain.package;
import stuff;
import package.FileB;
public class FileA extends Blah implements Listener {
/* global vars */
/* methods */
}
FileB.java is my data object class, which I'd like to reference from FileA.java thusly:
Map<Object, FileB> varname;
to be used along the lines of:
varname = new HashMap<Object, FileB>();
FileB.java, on the other hand, is defined as such:
package com.domain.package;
import stuff;
public class FileB {
/* global vars */
public FileB() {
/* stuff */
}
}
Why am I getting:
FileA.java:20: package package does not exist
import package.FileB;
? Rather, how do I make it work?
Because both files are in the same package (com.domain.package), you should not need to import FileB at all. You should be able to reference it directly.
Additionally, please ensure that both FileA and FileB are placed in their package folder: com/domain/package.
The package of FileB is com.domain.package. You are trying to use package.FileB instead.
package is a reserved word, don't use it as part of a package name. If you try to add a package with "package" as part of it in Eclipse, you will get an error message:
Invalid package name. 'package' is not a valid Java identifier
Rename the package, then remove the import statement. You don't need to import a file that's in the same package as the one it's referenced in.
#rgettman has the correct solution. Compiling both files using javac FileA.java FileB.javasolves this issue. You can also use his suggestion: javac *.java
Related
So I'm facing a cannot find symbol error when static importing an enum in a class that depends on it. They are both in separate files within the same directory. I've omitted an explicit package name.
TokenType.java
// No imports
enum TokenType {
ADD, MINUS, MULTIPLY, DIVIDE,
...
}
Scanner.java
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import static TokenType.*; // <--- (error: cannot find symbol)
class Scanner {
private static final Map <String, TokenType> keywords; // <--- (no error; javac can resolve the class name just fine)
static {
keywords = new HashMap<>();
keywords.put("+", ADD); // <-- (error: cannot find symbol, which makes sense)
keywords.put("-", MINUS);
...
}
...
}
I'm just not sure how to proceed. The names are all typed correctly, and there is only one TokenType class so there isn't a class conflict. My other classes in the project directory have no nested classes, do not extend/implement from other classes, or import libraries that have a TokenType class in their dependencies. I've cleaned my directory of all stale classes before each compile, and even changed the order in which I'm calling javac. Any help would be wonderful, thank you.
EDIT: Solution was to put them in a named package. Java doesn't allow imports from default package.
From the fact that the compiler can resolve the simple name TokenType in Map <String, TokenType>, it seems like TokenType is declared in the same package as Scanner.
You also said that you "omitted an explicit package name", which implies that both of these classes are not declared in the default package (static imports are not possible if they are in the default package), but some package with a name. Let's suppose that both of them are in
package com.example.foo;
Then you need to do:
import static com.example.foo.TokenType.*;
Note that even if you are in a location where the type is accessible with its simple name, you still need to use its canonical name (basically with the package name fully written out) in a static import declaration., and the name has to be qualified (in the form of X.Y), which is why classes in the default package don't work.
Here's how my directory look like:
practice(folder)
GraphTester.java
graph(folder)
Digraph.java
algorithm(folder)
TopologicalSort.java
I want to use graph.Digraph and graph.algorithm.TopologicalSort from GraphTester.java
What I try is this:
package graph;
public class Digraph
{
...
}
package graph;
// package graph.algorithm; <-- also doesn't work
public class TopologicalSort
{
...
private Digraph graph; // doesn't work
}
My question is, how can I use Digraph from inside TopologicalSort.java?
==== Update===
I tried the following, but still not working
package graph;
//package graph.algorithm; <-- this also didn't work
import graph.Digraph;
public class TopologicalSort
{
...
private Digraph graph;
}
I updated how the directory look like above. My intention was to use GraphTester.java as an outside class and not make it related to the package graph and graph.algorithm. But, it seems like putting it under the folder practice is causing the problem.
Put import practice.graph.Digraph; under your package declaration in TopologicalSort.java.
Make sure the package declaration for TopologicalSort is package practice.graph.algorithm;, it must match the directory structure.
package statement is used to create a package.
In order to use a package you need to use the import starement under the package statements follow by the name of the package you want to use.
You can also "import inline" packages, just a way to use clases or interfaces without import is to write the full path when you use it.
graph.algorithm.TopologicalSort ts = new graph.algorithm.TopologicalSort();
You can read the documentation here
add import statements.
import practice.graph.Digraph;
import practice.graph.algorithm.TopologicalSort;
How can one create a package in Java:
In a book i read its :
package package_name
public class whatever{}
.
.
.
But shouldn't this be enclosed in parenthesis such as :
package package_name
{
public class whatever{}
.
.
.
}
Just a minor confusion. Can anyone give me an example of the correct syntax?
The syntax for creating package
package package_name;
public class Whatever {
}
You can find more useful info Here.
When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.
Reference
Example :
package illustration; <------------
import java.awt.*;
public class Drawing {
. . .
}
Package is created as follows
package package_Name;
This package name has to be the first statement in the file.Once you declare package name start defining methods or classes or interfaces in it.
If this package you have to use in your any java file then write
import package_Name;
so that all the methods defined in the package will be accessible in java file.
No parenthesies. Package is nothing else then just a folder or set of folders. For example, if you create package named: utils, new folder will be created in your source folder. If you create package named org.utils, two folders will be created in your source folder. org and utils which will be inside of org folder. Also, every next package which starts with org. (for example org.ui) will be just a new folder created in org folder. So your folder hierarchy will look like this:
org--
|
utils
|
ui
Simple question but even though googled it a lot I could not find the answer.
Is it possible to import a class outside a package?
Let's say I have 2 folders A and B with a .java file in each, is it possible by using the clause import to import the class contained in A? import A.Aclass ? or it's mandatory using package syntax whenever there is the keyword import?
Yes it is possible to import the class with the import statement. For better understanding let's assume that you have three folders foldera, folderb and folderc where foldera contains a .java file named "ClassA.java", folderb contains another .java file named "ClassB.java" and folderc contains a .java file named "ClassC.java". Now, if you want to uses the member data and operations of "ClassA.java" in "ClassC.java" you can use the import statement as shown below:
import foldera.ClassA
If you want to use the member data & operations of "ClassB.java" in "ClassC.java" it is also possible with the import statement
import folderb.ClassB
As per the java source file declaration rule, if the class is a part of a package, the package statement must be the first line in the source code file, before any import statements that may be present. In this example, the first line of "ClassC.java" source file must be package folderc since it is located in folderc. Similarly, the first line of "ClassA.java" source file must be package foldera, and the first line of "ClassB.java" source file must be package folderb.
Hope now you are clear with the concept!
Thank you...
Well, if the class is defined to have a package a; then you need to import the class with the package name. If you have two packages which contain a class with the same name, then in your class which needs to invoke each of them, you will need to use a fully-qualified name. For example:
import a.Foo;
import b.Foo;
public class Bar
{
public static void main(String[] args)
{
a.Foo aFoo = new a.Foo();
b.Foo bFoo = new b.Foo();
}
}
Alternatively, if you have two packages with classes of the same name, you can simply skip importing them, but rather -- using them by their fully-qualified names (FQN-s).
If the class does not have a package ...;, then simply import it as:
import Foo;
However, if you have two packages (from different libraries) which contain classes with identical FQN-s, then the first one on the classpath will be picked.
Please, bear in mind that the convention for naming packages is to use lowercase letters and for classes -- the name should start with an upper case letter for each word in the class' name.
Yes it is possible.
If you have the following:
Package: PackA
Class: ClasA
Do:
import PackA.ClassA; //Import the class
OR
import PackA.*; //Import all the classes within the package
yes it is possible just import the package
syntax
import pck.ClassA or import pck.*
Yes, you have to use package syntax.
importing all class inside folder A.
import com.pack.A.*;
importing specific class inside folder A.
import com.pack.ClassName;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
eclipse 3.4 (ganymede) package collision with type
I'm new in java, but i tried to write a script for a game Lineage2.
heres a code:
package ZergZ.ZTeleport;
import javolution.util.FastMap;
import com.l2jserver.Config;
import com.l2jserver.gameserver.handler.IVoicedCommandHandler;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.model.Location;
import com.l2jserver.gameserver.handler.VoicedCommandHandler;
public class ZTeleport implements IVoicedCommandHandler
{
private static final String[] VOICED_COMMANDS =
{
"teleport"
};
#Override
public boolean useVoicedCommand(String command, L2PcInstance activeChar, String params)
{
if (activeChar == null)
return false;
if (params.equalsIgnoreCase("aden"))
{
activeChar.teleToLocation(147736,-56243,-2781);
}
if (params.equalsIgnoreCase("gracia"))
{
activeChar.teleToLocation(-186742,244167,2675);
}
if (params.equalsIgnoreCase("pvp1"))
{
activeChar.teleToLocation(147736,-56243,-2781);
}
if (params.equalsIgnoreCase("pvp2"))
{
activeChar.teleToLocation(179337,221937,4475);
}
}
#Override
public String[] getVoicedCommandList()
{
return VOICED_COMMANDS;
}
}
when server starts java says:
1. ERROR in \ZTeleport.java (at line 17)
package ZergZ.ZTeleport;
^^^^^^^^^^^^^^^^^^^^
The package ZergZ.ZTeleport collides with a type
the script is situated in ZergZ/ZTeleport.java
I'll give you another script which works fine:
package custom.HeroCirclet;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.quest.Quest;
import com.l2jserver.gameserver.model.quest.QuestState;
public class HeroCirclet extends Quest
{
______
}
Thanks.
You say "the script is situated in ZergZ/ZTeleport.java". This implies that the class ZTeleport belongs to the package ZergZ. But you have declared it as belonging to a different package, ZergZ.ZTeleport.
In your second example, I would bet that the source file is located in custom/HeroCirclet/HeroCirclet.java, which matches its package declaration, and does not create a naming conflict.
You either need to move the source file (people normally don't call java source files "scripts", btw) into a directory that matches its declared package, or change the package declaration to match its location.
It's because you have a class named ZTeleport in the ZergZ package and a package named ZergZ.ZTeleport.
The package name is basically the project directory where the Java file is situated.
That means if ZTeleport.java is in ZergZ directory, then the package name is
package ZergZ;
You don't specify the class name on package declaration and directory are separated with a . and not directory folder token.
The collision here is between your package name and your class name, which are the same. If you stick to the usual naming conventions (naming your packages with a starting lower case and your classes with a starting upper case), you should avoid such situations.
You should follow Java naming conventions. Change your package into:
package zergZ.zTeleport; // all name is begin with lower
// no change, but for clearer : all class name should begin with higher character
public class ZTeleport implements IVoicedCommandHandler
{
}
After that, for sure, you should refresh or/and rebuild your project to see it works.
Hope this help :)
If you see the error it says...
package ZergZ.ZTeleport;
^^^^^^^^^^^^^^^^^^^^
The package ZergZ.ZTeleport collides with a type
So basically you have created a conflict between the way you have named your class and the package. If you stick to java conventions you could name them as:
// Notice the first character of package name is small character
package zergZ.zTeleport;
// Class Name's first character is capital.
public class ZTeleport implements IVoicedCommandHandler{
..
}