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.
Related
When using java -cp path1:path2:path3 SomeClass to execute a class, is it possible to make java report the jar or class filename and pathname which it finds for each package imported in the class?
No ... and partly yes.
No because "importing" is a compile time concept only. There is no need to import a class in order to use it, and an import statement doesn't actually mean that the class is actually used. Also, you don't import a package: you import classes from a package, or static members from a class.
Partly yes because the java -verbose:class option will log each class that gets loaded by the JVM.
And if you want to statically find all of the other classes that are directly referenced by a given class it is possible to get this by analyzing the classes .class file.
I have the In package from Princeton loaded into the same directory as my files, and I compiled it.
And, I use the package in my code. But, when I use import In; somehow I still get an error?
java:7: error: '.' expected
import In;
^
What is the solution to this silly problem?
The code you linked to has no package.
Just delete import In, and somewhere in your code create an instance In myIn = new In(myUrl);, and you should be good.
Alternatively, modify your copy of "In.java" and make it the same package as you're using for the rest of your code.
Look at the main() in the code for examples of how to use class "In".
In order to fix this problem, you either need to add a package declaration to In.java that is the same as your package (and then simply omit your import statement), or (my recommendation) you need to add a package declaration to In.java that is different than your package (and move it to the corresponding folder), and then import In by the name of the package that you've given it.
To make this more concrete:
Add the following to the top of In.java:
package edu.princeton.cs.introcs.in;
Then move it to the corresponding "edu/princeton/cs/introcs/in" directory.
(Create it if it doesn't exist).
Then, in your file, import it by its qualified name:
import edu.princeton.cs.introcs.in.In;
Note that, in both of the cases above, you'll need to compile In.java along with your code that uses it (or you need to compile In.java, first, and ensure that it is on the classpath when compiling your code that uses it), and at runtime, you need to bundle In.class with your code (e.g. in the JAR you produce) or similarly guarantee that the byte code for that class (either the .class file or a JAR containing the .class file) are on the class path when executing your compiled code.
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
I'm not sure how to import a file from a directory above. That is, I have a setup like so
directory: MyProject
Main.java
directory: Other
Other.java
Basically, Main.java is in "MyProject" and Other.java is in a folder inside the project's root folder. I can easily do
import Other.*;
to get those files available in Main, but how do I get Main.java to be visible to Other.java?
import ../Main.java
Obviously this doesn't work, but that's the general functionality I'm looking for. Any suggestions? I would prefer not having to use absolute paths. Thanks!
Edit: I meant import not include. Sorry. Been using C++ too much.
Java does not include files. You can however directly use classes using the simple name by using import statements.
Basically you need a file per (top level) class you define. This allows IDE's to rename compilation units, and do other refactorings. Besides that, it lets you easily add code at the right spot.
Java does use packages to create namespaces. Packages themselves are completely separate namespaces. Although the namespace seems to be a tree structure, in Java each package is actually not related to any other package. Hence you cannot use it as a folder structure, using .. is not allowed. This may change once "super packages" are introduced.
The Java import statement looks a lot like #include, but the name change is deliberate: instead of grabbing the file to make the definitions in that file known, it is simply a statement to make it easier to refer to classes and interfaces. It has no other effect than having a shorter name to a class (or, for import static, constants and other static members).
Most of the time the top level classes are represented using a folder structure that reflects the package name. This makes it easy for IDE's and developers to find the file representing the class. It also makes it easy to put in version control. It is however not part of the Java specification itself; the location of Java source and classes is not defined. Earlier IBM IDE's actually stored Java source and classes in a database for instance; they did not use files at all. Newer IDE's such as Eclipse may use different source folders, e.g. one for Unit test files and one for the library itself.
So finally, the only way to include packages is by specifying the full package name, then a dot and then the class to import, or the * wildcard to import all classes of that package.
import java.util.Vector;
import java.util.*;
Most IDE's will create these import statements for you, possibly after you have chosen the right class to import (in case there are classes with the same name in different packages).
More information can be found in the Java Language Specification (Java 7 version).
In your case you have defined a Main class in the root or default package which is strongly discouraged. You can directly refer to Main without any import statement. The Other class is in the identically named Other package (using uppercase in package names is strongly discouraged as well). You can refer to it by using import Other.Other.
include ???
Java doesn't have file source inclusion support, it rather use a naming conversions, so you should import the namespace (package) that you need in your source file.
You should define a package for your main class and then import it in the Other class .
the Main.java is in the default package, this is impossible to import from other (named) packages
put it in a package and import as normal
directory: MyProject
directory: base
Main.java
directory: other
Other.java
(also package names are lowercase normally)
if you have file outside of your project it means this file:
wouldn't be compiled by project
wouldn't get into jar
can't be used in runtime
so you really shouldn't include it.
Either move it into project, or include dependent project which contains that file.
Java is not like C++. You include by package name. So if toplevel file is in project AAA in folder src/aaa then you should include that project as dependent jar and refer to file as import aaa.Main
I think import Main; should just work.
You should read up java concepts package and classpath. Please look at the documentation here. The options that will work for you are sourcepath and classpath.
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?