2

I am trying to create a java file that will read in xml files but for me to do this i need to use a DOM Parser which involves creating a package and declaring it and i was just wondering how do you do that

user5223558
  • 29
  • 1
  • 2
  • 1
    Use an IDE like eclipse instead of notepad – Jens Aug 13 '15 at 12:54
  • Please, read the [Java Docs on Packages](https://docs.oracle.com/javase/tutorial/java/package/) to get a better understanding of what packages are. – Diyarbakir Aug 13 '15 at 12:55
  • Do you want to `import` the `DOM Parser` package you can do that like this(at the top of your class underneath the package declaration for your class): `import whatever.whatever.whatever.DOMParser.*;` – brso05 Aug 13 '15 at 12:59
  • @Jens, IDE will help him, but here he must understand what are `packages in java` – Andrew Tobilko Aug 13 '15 at 13:13
  • @AndrewTobilko But that are the basics of java. – Jens Aug 13 '15 at 13:25

2 Answers2

4

Packages in Java classes reflect the folder structure of your project. For example, if your project is located in the project folder, and the structure of your project is:

project\
project\mypackage\
project\mypackage\MyClass.java

then the MyClass.java file should contain the corresponding package declaration:

package mypackage;

public class MyClass {

You should also read a tutorial on packages to better understand how they work: https://docs.oracle.com/javase/tutorial/java/package/index.html

Forketyfork
  • 7,416
  • 1
  • 26
  • 33
0

A package is declared as the first non-comment line in your Java source file:

package an.example.pkg;

public class Foo {
  // code here
}

Will declare a class named Foo in a package called an.example.pkg.

Of course, the package is associated with a folder on your file system, so your .java file would actually need to be located in a directory called <some directory>/an/example/pkg in your file system (where <some directory> is a directory that is in your Java classpath).

The package tells Java where to search to find your classes, i.e. in which folders relative to your classpath to look.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Bobby StJacques
  • 724
  • 4
  • 9