Меню Закрыть

Java error package does not exist

I am trying to import a package I created, my file structure is as follows:

In my tester I import the package using: import mypackage.Cram; but when I try to compile my tester.java I get the following error: package mypackage does not exist. Any help would be much appreciated. Here is the error; algo is my tester.java and pack is mypackage.

2 Answers 2

If you are compiling all of your files at the same time, then you should just be able to use your code as-is, provided that you have your classes defined properly and in the directory structure as indicated by your package keyword.

Suppose you have the following directory tree (as in your original post):

And the following classes defined:

You can compile all of these files into a single directory (usually, IDE’s will compile to a "classes" or "bin" directory) with the following command:

This will place all your class files in the directory "classes"

You can then run by using:

And produce output:

If you are compiling your package separately from Tester.java, like a library, then you can do the same thing, just with some separate commands.

This will put the .class files in the same classes directory. When you try to compile Tester.java (which uses mypackage.Cram ), you simply tell the compiler where your classes are:

produces the same output.

My guess is, your classes and names are all mangled, and Java expects them to follow a convention.

Dear members,
I am developing a Java web project. in my ‘src’ folder- I have the following two classes:

First, from my project root directory I compiled the model class:
E:SCWCD preparationHF ProjectsjavaEbook>javac -cp "C:My Toolsapache-tomcat-
6.0.20apache-tomcat-6.0.20libservlet-api.jar" -d classes srccomfoomodelBookExpert.java

Читайте также:  Зачем клеят изоленту на фары крестом

But later when I tried to compile the controller servlet:
E:SCWCD preparationHF ProjectsjavaEbook>javac -cp "C:My Toolsapache-tomcat-
6.0.20apache-tomcat-6.0.20libservlet-api.jar" -d classes srccomfoocontrollerBookSelectServlet.java
I got the following errors:
srccomfoocontrollerBookSelectServlet.java:8: package com.foo.model does not exist
import com.foo.model.*;
^
srccomfoocontrollerBookSelectServlet.java:25: cannot find symbol
symbol : class BookExpert

though the model />

please anyone help me out of the pain i am suffering from the last night

So there’s a folder /usr/share/stuff in the root directory

in stuff there are a bunch of java files with package org.name definitions at the top

I am running javac test.java where test.java is in a subdomain

I added /usr/share/stuff to my class path.

and at the top of test.java I add import org.name

But I get a package does not exist error. why?

6 Answers 6

Are they in the right subdirectories?

If you put /usr/share/stuff on the class path, files defined with package org.name should be in /usr/share/stuff/org/name .

EDIT 2: Sorry, I hadn’t realised you were talking of Java source files in /usr/share/stuff . Not only they need to be in the appropriate sub-directory, but you need to compile them. The .java files don’t need to be on the classpath, but on the source path. (The generated .class files need to be on the classpath.)

You might get away with compiling them if they’re not under the right directory structure, but they should be, or it will generate warnings at least. The generated class files will be in the right subdirectories (wherever you’ve specified -d if you have).

Рекомендуем к прочтению

Добавить комментарий

Ваш адрес email не будет опубликован.