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
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 am trying to sort my classes into packages but i can't import them.
My files are in the following folders:
- .java files are in C:\Java\Code\src\my\app\Timer
- .class files are in C:\Java\Code\compiled\my\app\Timer
In my class (timer) i've added package my.app;
Also, I have setted the CLASSPATH to look in both src and compiled folders.
Then, I have another folder where I put my "bigger" projects in:
- C:\Java\Projects\myProject
The problem is that when I try to import the class Timer into MyProject using import my.app.*; all I get is:
Error: package my.app does not exist
Culd you please give me a hand?
PS. My IDE is Dr.Java
I have found the problem.
It appears that Dr.Java ignores complitely the CLASSPATH variable. It is necessary to set in preferences where are the .class files.
I am new in Java and Eclipse.
I am trying to import external class files in my code. I right click on project src->properties->buildpath->configure build path-> libraries->add external class folder
by doing this default package appears in referenced libraries, having all the Java class files, but I still can't import my file.
Next I zip all .class files in a zip folder and add as external jars, by doing this all class files are shown under default package, under referenced libraries, and still I can't import my class files into code.
How can I rename the default package, so that I can access and import the class files?
By right clicking the classes, under the default package in the referenced libraries, there comes no option of refactor and move.
You have to respect the File System Hierarchy in which the class files come from.
Let's assume that the class files (For example the class Clazz) have the declaration package ch.charno.xy, they must remain in a folder hierarchy ch/charno/xy/Clazz.class. You have to import the folder in which the subfolder ch resides so that the correct hierarchy is preserved. It's not possible to just import the class files, but you need to import the whole folder hierarchy.
(I hope I understood your question)
Basically I wish to use the methods of a class within the Jar file, which looks like this:
Can somebody please tell me what I need to import in order to use those methods and variables?
You don't need to import anything.
Jar files aren't imported, they are added to the classpath.
From the screenshot you've posted, we can see that the myJar.jar file is included in your eclipse classpath, so there's nothing more to do there.
Classes are imported, if they are in a different package.
Your Examplew class is in the default package. BMIcalculator is also in the default package. Because they are the same package, you don't need to import it.
You should be able to simply make references to BMIcalculator from within Examplew. Just try it.
Try compiling this code - it should work:
public class Examplew
{
private BMIcalculator calc = new BMIcalculator();
}
You might get warnings about the unused private field, but you can ignore that for now.
If that doesn't work for you, then please post the error, because it doesn't look like the problem is with your imports (or your classpath)
Quote from this question:
You can’t use 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;
That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.
If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.
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?