Java Package Does Not Exist Error

Java Package Does Not Exist Error

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: If you don't already know this, you should probably read this doc about understanding classpath.

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).

You should use something like javac -sourcepath .:/usr/share/stuff test.java, assuming you've put the .java files that were under /usr/share/stuff under /usr/share/stuff/org/name (or whatever is appropriate according to their package names).

package does not exist error!

This works:

com/example/model/BearExtra.java
package com.example.model;

public class BearExtra {
public static void go() {
System.out.println("Yay, it works!");
}
}
com/example/web/Bear.java
package com.example.web;

import com.example.model.*;

public class Bear {
public static void main(String[] args) {
BearExtra.go();
}
}

Now, to compile and run these classes, go to the directory where you can "see" the com folder and do:

*nix/MacOS
javac -cp . com/example/model/*.java com/example/web/*.java
java -cp . com.example.web.Bear
Windows
javac -cp . com\example\model\*.java com\example\web\*.java
java -cp . com.example.web.Bear

and the following is being printed to the console:

Yay, it works!

java package <package> does not exist -> Java Error

If you have a directory structure like this:

mySrc
controle
modelo
visao

You need to compile from the "top" of the directory structure, and you need to specify a full path to the file. You also need the files in modelo and visao compiled so the .class files are available.

myScr> javac controle\BibliotecaMusicalControle.java

\ is a file separator on Windows. On Mac and Unix it's /.

Java "package does not exist" error in UNIX

Thank you guys! I just found out running "ant -buildfile build.xml" and then "ant run -buildfile build.xml" does the magic for you!

Java: package does not exist

From your error it looks like your "other program" B.java is not in the same directory (E:\stuff\Java) of 'A.java'. This means that when you try to compile B.java the compiler does not know where to find class pack.A. To "make A visible" you must add pack.A to your classpath, which means compiling with:

javac -cp ".;<path_to_add>" B.java

In your case <path_to_add> should be E:\stuff\Java. This sets your classpath to not only the current directory (.) but also the directory where your pack package is.

To run your program you again have to add pack.A to you class path:

java -cp ".;<path_to_add>" B

Where again <path_to_add> should be E:\stuff\Java.

Here I am assuming you are using windows. On Unix the -cp option as a slightly different syntax: -cp ".:<path_to_add>" where the ; has been replaced by :.

package does not exist when running the application

I'm afraid it may be a problem with your IDE.
Check if you have the last version and try to run also with other (Eclipse,STS,...).
I hope this help you.



Related Topics



Leave a reply



Submit