I am very new to java.
Can somebody help why I am getting error: cannot find symbol error.
I have a class User.java which I am able to compile with success. User.class gets created in the same dir.
When I try to compile Another class UserDao.java which used User, it reports cannot find symbol error as stated.
vm#vm:~/UserManagement/com/tutorialspoint$ pwd
/home/vm/UserManagement/com/tutorialspoint
vm#vm:~/UserManagement/com/tutorialspoint$ ls
User.class UserDao.java User.java UserManagement.war UserService.java web.xml
vm#vm:~/UserManagement/com/tutorialspoint$ javac UserDao.java
UserDao.java:15: error: cannot find symbol
public List<User> getAllUsers(){
^
symbol: class User
location: class UserDao
UserDao.java
package com.tutorialspoint;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public List<User> getAllUsers(){
List<User> userList = null;
try {
Please help.
Thanks.
You did not set a classpath, which defaults to be the current directory. So javac can't find the .class file for User.
Please take the effort to read the classpath documentation.
The solution here is to either compile all classes at once:
javac UserDao.java User.java UserService.java
Or set the classpath to the root of your packages:
javac -cp "/home/vm/UserManagement" UserDao.java
Related
I have this class that I want to import into another class that is outside the previous class folder.
So, I have a GoogleDriveAPI class, which I want to import to DocumentServices class.
on top of my GoogleDriveAPI class there is this line
package org.ofbiz.ClientManagementServices;
but when I try to import it to DocumentServices class with this line below
import org.ofbiz.clientmanagementservices.GoogleDriveAPI;
I get this error below,
error: package org.ofbiz.clientmanagementservices does not exist
[javac17] import org.ofbiz.clientmanagementservices.GoogleDriveAPI;
What might be the problem with my import because I am 100% sure I am doing the right thing?
Java packages are cas sensitive.
You should change the import to :
import org.ofbiz.ClientManagementServices.GoogleDriveAPI;
I am trying to run my java class by command line. When I executed throw an error:
PS C:\Users\mcruz\Desktop\diff-java> javac src/main/Main.java
src\main\Main.java:8: error: package com.google.gson does not exist
import com.google.gson.Gson;
^
src\main\Main.java:10: error: package object does not exist
import object.Command;
^
src\main\Main.java:11: error: package object does not exist
import object.Environments;
^
src\main\Main.java:12: error: package object does not exist
import object.MetadataType;
^
src\main\Main.java:13: error: package object does not exist
import object.PackageBuild;
^
src\main\Main.java:14: error: package object does not exist
import object.ReadFile;
^
src\main\Main.java:16: error: package object does not exist
import object.Resource;
I don't know if this command detects the libraries that I implement in the project or what I need to do to solve this problem. (My libraries are in a folder with the name lib.)
I read in another post that I can use javac -cp to add the classpath but, I need to add more than one library.
Example :
javac -cp lib/error_prone_annotations-2.11.0.jar;lib/gson-2.9.0.jar;lib/logger-1.24.0.jar;lib/logger-slf4j-1.24.0.jar;lib/logger-spi-1.24.0.jar;lib7safe-logging-1.24.0.jar;lib/slf4j-api-1.7.31.jar src/main/Main.java
package main;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import com.google.gson.Gson;
import object.Command;
import object.Environments;
import object.MetadataType;
import object.PackageBuild;
import object.ReadFile;
import object.Resource;
public class Main { ........
Here's the code for the source file:
package moa4;
public class Book {
....
}
And for the destination file:
import moa4.Book;
public class Library {
...
}
The source and the destination are both saved in the same directory with the address:
C:\Users\\java\M\moa4
I'm getting the following error: package moa4 does not exist
You asked Library to import a package moa4.Book but you defined no such package. Instead, you defined a type Book inside package moa4, and that is not consistent with your import directive.
You could either import the package, or make that an import static of the class, but since Book and Library are both in the same package you don't need the import directive at all.
As mentioned, C:/Users/java/M needs to be in your classpath ("-cp" option).
I am using one .java file and i have given a package name as com.onlinmebank but netbeans displaying error at this package declaration line as Incorrect Package.
Following is the package declaration code.
package com.onlinebank;
import java.sql.*;
import java.util.*;
public class BankCommons{
//All Code Here
}
Can Anybody tell me why i am getting this error
All Java keywords are lower-case!
So, this should work:
package com.onlinebank;
import java.sql.*;
import java.util.*;
public class BankCommons {
//All Code Here
}
And keep in mind, that the file BankCommons.java must be placed in the directory com/onlinebank.
In package, p should be small not Package(no captial P).
Also in public class p should be small. Similarily for Import also.
Important note all java keywords are in lower cases
Need Some help. Though there are lot of different answers available and also I tried them but couldn't make it work. I intsalled hadoop locally in my mac os and when I tried compiling the java programs I got the following errors. I know the problem is with the setting up the correct class path, but in may case providing the class path didn't make it work. I have installed hadoop under /usr/local/Cellar/hadoop/1.2.1/libexec
I have my java home set to export JAVA_HOME="$(/usr/libexec/java_home)"
and class path set to export HADOOP_CLASSPATH=${HADOOP_HOME}/bin:${JAVA_HOME}/bin:${PATH}
but still getting the below errors. Any suggestions for the setting up the correct class path would be appreciated.
LineIndexer.java:6: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.LongWritable;
^
LineIndexer.java:7: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.Text;
^
LineIndexer.java:8: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.FileInputFormat;
^
LineIndexer.java:9: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.FileOutputFormat;
^
LineIndexer.java:10: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.FileSplit;
^
LineIndexer.java:11: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobClient;
^
LineIndexer.java:12: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobConf;
^
LineIndexer.java:13: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.MapReduceBase;
^
LineIndexer.java:14: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.Mapper;
^
LineIndexer.java:15: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.OutputCollector;
^
LineIndexer.java:16: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.Reducer;
^
LineIndexer.java:17: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.Reporter;
^
LineIndexer.java:21: cannot find symbol
symbol : class MapReduceBase
location: class LineIndexer
public static class LineIndexMapper extends MapReduceBase
^
LineIndexer.java:22: cannot find symbol
symbol : class Mapper
location: class LineIndexer
implements Mapper {
^
LineIndexer.java:22: cannot find symbol
symbol : class LongWritable
location: class LineIndexer
implements Mapper {
^
LineIndexer.java:22: cannot find symbol
symbol : class Text
location: class LineIndexer
implements Mapper {
^
Looks like your classpath is wrong, try this instead:
javac -classpath /usr/local/cellar/hadoop-1.2.1/hadoop-core-1.2.1.jar
Or redefine your HADOOP_HOME env variable to be /usr/local/cellar/hadoop-1.2.1