I have a file structure as follows:
src/cs/example/Hello.java
src/cs/utility/HelloHelp.java
bin/cs/example/Hello.class
bin/cs/utility/HelloHelp.class
The package and import statements in Hello.java are:
package cs.example;
import cs.utility.MyMethods;
While the package statement in HelloHelp is:
package cs.utility;
I attempt to run Hello (that uses a method from HelloHelp):
java -cp src/cs/utility src/cs/example/Hello.java
src/cs/example/Hello.java:2: error: package cs.utility does not exist
import cs.utility.HelloHelp;
^
src/cs/example/Hello.java:10: error: cannot find symbol
int max = HelloHelp.borp(intOne,intTwo);
^
symbol: variable HelloHelp
location: class Hello
Any help on resolving this issue would be very useful, thank you!
In your java -cp call you are pointing to your .java files instead of your .class files in /bin.
Try changing your java command to use the class files instead. Something like the following should work:
java -cp "bin/*" cs.example.Hello
Related
This question already has answers here:
How to compile java package structures using javac
(6 answers)
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 2 years ago.
I have been trying to compile through CMD by creating packages manually. I tried the command javac -d . Nameinfo.java and javac -d . Returndata.java, where Nameinfo contains the calculations and Returndata contains main function, and this command worked absolutely fine. Now I made 2 different packages in my "java programmes" folder i.e packx and packy, where packx contains Nameinfo and packy contains Returndata. Now, when I compile these two, Nameinfo.java got compiled but Returndata could not read/recognize the imported Nameinfo in Returndata.java.
errors:
E:\java programmes>cd packx
E:\java programmes>cd packx
E:\java programmes\packx>javac Nameinfo.java
E:\java programmes\packx>cd..
E:\java programmes>cd packy
E:\java programmes\packy>javac Returndata.java
Returndata.java:2: error: package packx does not exist
import packx.Nameinfo;
^
Returndata.java:7: error: cannot find symbol
Nameinfo data = new Nameinfo();
^
symbol: class Nameinfo
location: class Returndata
Returndata.java:7: error: cannot find symbol
Nameinfo data = new Nameinfo();
^
symbol: class Nameinfo
location: class Returndata
3 errors
if java or javac needs, say, packx.Returndata as a type to do something, it will scour a classpath and/or sourcepath for a directory named packx, and within that, Returndata.java or Returndata.class depending on the tool and path we're talking about.
So, you're in E:\java programmes\packy, and you're compiling some code that imports packx. Therefore, javac will look in the classpath for packx/Returndata.class and in the sourcepath for packx/Returndata.java and it doesn't find what you want because E:\java programmes isn't on the source path; generally they default to . (i.e. the current directory).
Stay in the E:\java programmes folder for all this work:
E:
cd "\Java programmes"
javac packx/Nameinfo.java
javac packy/Returndata.java
Or better yet, realize that manually compiling a multipackage java application is crazy. Use maven, gradle or some other build tool that takes care of all this for you :)
I need a particular JAR file, fo execute a code. I am able to run the code In eclipse by adding the JAR file to libraries.
But when I run he code from cmd prompt I get error saying unable to import javax.jms(the jar file).
C:\tibco\ems\8.5\samples\java\JNDI>javac tibjmsJNDIFT.java
tibjmsJNDIFT.java:43: error: package javax.jms does not exist
import javax.jms.*;
^
tibjmsJNDIFT.java:108: error: cannot find symbol
ConnectionFactory connectionFactory =
^
symbol: class ConnectionFactory
location: class tibjmsJNDIFT
tibjmsJNDIFT.java:109: error: cannot find symbol
(ConnectionFactory)jndiContext.lookup(factory[j]);
^
symbol: class ConnectionFactory
location: class tibjmsJNDIFT
3 errors
C:\tibco\ems\8.5\samples\java\JNDI>
You should use the "-cp" or "-classpath" option:
javac -cp ".:/path/to/jms.jar:/path/to/any-other.jar" tibjmsJNDIFT.java
On Windows OS you might have to use "semicolons" (;) instead of "colons" (:)
My project runs smoothly in Eclipse, no errors at all. I ran it through local tests (that are equal to the ones in an online mooshak contest) and all checks out. But when I sumbit it online, I get a CompileTimeError.
To try to locate the problem, I attempted to use javac in Main.java, and this happened:
javac -encoding US-ASCII Main.java
Main.java:8: error: package code does not exist
import code.*;
^
Main.java:129: error: cannot find symbol
public static void addWords(Scanner in, LibSystem system, int wordCount)
{
^
symbol: class LibSystem
location: class Main
Main.java:153: error: cannot find symbol
public static void searchWord(LibSystem system, String word) {
^
Other symbol errors follow for every reference to classes from my only package, which I fully import and the header of Main, for a total of 29 errors.
Project
src
Main.java
code (package with all my other java files)
Follow these steps :-
1.) make sure LibSystem class is in classpath.
2.)Compile java files in code folder using -d option. That will create directories with classes.
javac -d . code/*.java
3.) then compile your Main class.
javac Main.java
Consider the following java program:
import computer_package.Computer;
import com.sun.jna.Library;
public class HelloWorld {
public static void main(String[] args) {
Computer computer = new Computer();
System.out.println("Hello, World");
}
}
and the following directory structure:
Rebs-MacBook-Pro:jalint2 rebcabin$ find computer_package/
computer_package/
computer_package//Computer.class
computer_package//Computer.java
wherein Computer.java is as follows:
package computer_package;
public class Computer {
public Computer() {
System.out.println("Constructor of Computer class.");
}
public void computer_method() {
System.out.println("Power gone! Shut down your PC soon...");
}
}
and another directory with just jar files:
Rebs-MacBook-Pro:jalint2 rebcabin$ find com
com
com/sun
com/sun/jna
com/sun/jna/jna-4.0.0.jar
com/sun/jna/jna-platform-4.0.0.jar
If I comment out just the import com.sun.jna; line in the original HelloWorld.java file, then all works well
Rebs-MacBook-Pro:jalint2 rebcabin$ javac -g HelloWorld.java
Rebs-MacBook-Pro:jalint2 rebcabin$ java HelloWorld
Constructor of Computer class.
Hello, World
Javac and java are able to find the class file ./computer_package/Computer.class without any further hints or annotation or classpath-hacking.
Now, if I restore the import com.sun.jna; line and comment out just the import computer_package.Computer; line and the line the constructs a new Computer, and compile like this:
Rebs-MacBook-Pro:jalint2 rebcabin$ javac -g -cp "./com/sun/jna/jna-4.0.0.jar" HelloWorld.java
or even like this:
Rebs-MacBook-Pro:jalint2 rebcabin$ make
javac -g -cp "./com/sun/jna/jna-4.0.0.jar:./computer_package" HelloWorld.java
then all is still well:
Rebs-MacBook-Pro:jalint2 rebcabin$ java HelloWorld
Hello, World
At least, the import com.sun.jna; line didn't throw a compiler error -- admittedly, I am not yet using the jna library; I'm just trying to build up to that momentous achievement in baby steps.
So if I now cautiously uncomment the import computer_package; line and the line that constructs an instance of Computer, yielding the HelloWorld.java file at the top of this question that tries to use both my Computer class and jna, all hell breaks loose; the previously acceptable Computer class is now inaccessible:
Rebs-MacBook-Pro:jalint2 rebcabin$ make
javac -g -cp "./com/sun/jna/jna-4.0.0.jar:./computer_package" HelloWorld.java
HelloWorld.java:20: error: package computer_package does not exist
import computer_package.Computer;
^
HelloWorld.java:25: error: cannot access Computer
Computer computer = new Computer();
^
bad class file: ./computer_package/Computer.class
class file contains wrong class: computer_package.Computer
Please remove or make sure it appears in the correct subdirectory of the classpath.
2 errors
make: *** [HelloWorld.class] Error 1
I know this is elementary; I am not a java expert and I admit I am trying to guess my way around the classpath nightmare world; I am using just emacs and bash, avoiding the IDEs that add even more layers of opaque abstraction.
You need to specify the classpath as the directory containing the package, not the package's classes, so if you're running from the same directory:
javac -g -cp "./com/sun/jna/jna-4.0.0.jar:./"
Note that, when you don't specify a classpath, the current directory is used by default. See http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
Hi and thanks for taking the time to answer my questions.
I have two files in my root folder (~/). The Main.Java and TestMain.java. Main.java compiles and runs smoothly. TestMain on the other hand does not. TestMain is basically a test class where I use JUnit to handle different scenarios. I instantiate Main in TestMain but the problem is that the compiler cannot find Main.java.
Here's the code:
user#linuxbox ~ $ javac -cp junit-4.10.jar TestMain.java
TestMain.java:8: error: cannot find symbol
Main mainClass = new Main();
^
symbol: class Main
location: class TestMain
TestMain.java:8: error: cannot find symbol
Main luckyStrings = new Main();
^
symbol: class Main
location: class TestMain
2 errors
How can I make the Main class available to the MainTest.java class? Thanks so much!
In your classpath option, you have set the classpath to only junit-4.10.jar. You must also include the current directory where your Java files reside.
javac -cp "junit-4.10.jar:." TestMain.java
This includes two paths -- JUnit and the current directory, separated by a :. (If this were Windows, then you would use a ; as a separator).
Just another input...
-d can be used to specify the target directory where the compiled class files should be put
javac -d . -cp "junit-4.10.jar:." TestMain.java