Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package not found; javac

Tags:

java

javac

This is annoying.

I have a directory structure like this

-lib
   --some jar files

-packageName
   --Main.java
   --SomeOtherPackage
      --SomeOtherJavaClass.java

Main.java imports SomeOtherPackage. And both java files uses jars in the lib.

What I do is add the jar files independently in the CLASSPATH. And then run as: javac packageName/Main.java

but it gives the error that Package not found SomeOtherPackage . Shouldn't it automatically realize the dependency and build SomeOtherPackage as well? What would be the javac command and the classpath for the above case?

Thanks

like image 615
neebz Avatar asked Jun 14 '10 23:06

neebz


People also ask

How do you fix Java package does not exist?

Add the jars in the Java Transformation under Java Code > Settings > Add Classpath to use classes from c:\sample\Myjar. jar jar file. Add the import statement that corresponds to the class file in the jar. import com.

Where javac is found?

The javac.exe file is located in the bin folder of the JDK. The reason behind to occur the error is that the PATH is not added to the System's environment variable. If the PATH is not added to the environment variable or not properly set, we cannot compile the Java application.


1 Answers

The normal practice is to add the package root to the classpath.

When you're already in the package root, use -cp .. E.g.

cd /path/to/all/packages
javac -cp . packageName/Main.java

If you want to include JAR files as well, use the ; (or in *nix, the :) as classpath path separator:

javac -cp .;lib/file.jar packageName/Main.java

To save the time in repeating all the typing of shell commands, use a .bat (or in *nix a .sh) file. Or just an IDE if you're already familiar with java/javac and so on.

like image 127
BalusC Avatar answered Nov 07 '22 21:11

BalusC