I have maven project which src folder contains 2 packages and 1 class outside this packages.
Inside MainWindow i want to create ClientConnection instance but it cant resolve symbol 'ClientConnection'. Is this possible to create this instance without moving file inside 'GUI' package ?
Create a new package (e.g. connection) in java folder and move ClientConnection to package conenction. In your MainWindow add
import connection.ClientConnection;
Classes in the default package cannot be imported by classes in packages.
You need to create a package for all your classes. It's best practices.
Related
I've created a default package xyz and within this package I have created a folder x so now there is a package by the name of xyz.x. Now in the main file of the regular package xyz I am trying to make it extend a class located inside of that folder xyz.x.
How do I do this? and How do I import that folder? I've already went to the Project properties, then libraries and under compiler, added a folder path to the folder I've just created.
If you created a project netbeans just use the project path to reach your desired Class:
package javaapplication5.xyz;
import javaapplication5.xyz.x.InsideX;
public class InsideXYZ extends InsideX {
}
I'm trying to import a package to a class to make some tests creating a particular class and to instance some objects.
So, I create a source folder (asdsad) and put a class in there, after I tried to import Produtos.GestaoDeContrato.Mapeamentos.Telas.* but doesn't work.
Is it possible to do this?
UPDATE 01
Thanks for the answers, I tried to do this, but I don't know if I did what you said, that is correct? Because doesn't works
Please, pardon my english.
If the class you are trying to instantiate is in a separate project as I can see, then you need to to add this class to your current's project class path.Or you can place the class in the same project but in a different package !
You need to place all the java classes in the src folder of the project in eclipse unless you are trying to import from the jar file
but if you have multiple projects you can go to properties>build
path> Libraries > Add Class Folder > The select the required java
package of the proj you need.
I created a public class under default package and then I exported this class as jar file. I then created a new project and added the jar file from my directory as a library in my new project.
My problem is, why the class in the jar file cannot be accessed outside the default package ? As shown in the screen shot there is an error in my test.java class under test package when I try to access the class from the jar file.
Any solution to this problem?
Thank you
You can not access classes in the default package from a named package.
Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:
import Unfinished;
This is no longer allowed
See https://bugs.openjdk.java.net/browse/JDK-6975015
My problem is very simple to describe.
I've made a Class named Car and I let IntelliJ build the Car.class file.
After that I've made another Project which has a dummy class called Main which wrapps this Car object.
In the project structure I've added as an external library the folder which contains Car.class.
So basicly after declaring the Car member variable in my Main dummy class, IntellJ normaly suggests me to import Car which is shown in the picture below.
Picture1
When I choose to import Car class it directly writes down the package before the Class name (not the usual import statement) and the import fails as shown on this picture.
Picture2
(note: the reason it states org.dino.test.pojos is because Car class package is org.dino.test.pojos)
The project setup is shown below.
Picture 3
Does someone know how to setup this project structure so such things work?
The fact that IntelliJ has marked the org directory as the library home is suspicious- I suspect that you have defined the classes in the library as the org subdirectory rather than the JAR or classes directory itself.
You also have a folder structure that is inconsistent with your package structure.
When you define the library, select the testLibrary directory/JAR rather than one of the directories inside it. Also, ensure that the folder structure inside your library matches the Java package of the class within it.
I have a jar called "MyTools". The jar is in c:\data folder. I created a new file in the same folder called "UseTools.java". Now I would like to use some of the classes from the MyTools.jar in my UseTools.java. I tried this but it doesnt seem to work:
import MyTools.*;
public class UseTools
{
public static void main(String[] args)
{
MyTools.SomeClass foo = new SomeClass();
SomeClass.doSomething();
}
}
I tried to compile this with:
javac -cp . UseTools.java
and got this error message:
UseTools.java:1: package MyTools does not exist
import MyTools.*;
^
UseTools.java:7: package MyTools does not exist
MyTools.SomeClass foo = new SomeClass()
^
2 errors
I did not set the package name in any class.
Do I have to set a package name in my jar classes?
To mention something that relates more to the title of the question:
In Java, you can't access classes in the default package from code within a named package.
This means, if the classes in your jar file do not belong explicitly to any package and inside the jar your files are directly in the root folder without subfolders, they are in the default package. This is not very elaborated and lacks modularity as well as extensibility, but is technically alright.
Then, you can only use these classes from code which also is in the default package. But this does not necessarily mean it has to be in the same jar. If you have multiple src or class folders they could all contain classes in the default package which can interact. The organization in JAR files and the package structure in your project are independent of each other.
However, I'd strictly encourage you to use explicit package information.
In your MyTools.jar there should be a package with the name MyTools. And before compiling you should add the jar to the classpath.
You need to add -cp file.jar instead of -cp .
The latter one will pick up .class files only. BTW: why not using an IDE like netbeans, eclipse or intelliJ?