Java can't find symbol from .class in the same directory - java

I have two files: MyStringLog.java (which extends ArrayStringLog) and TestDriver.java in the same directory. Why doesn't TestDriver.java recognize the MyStringLog constructor from the MyStringLog class? In TestDriver.java I declare:
MyStringLog animals = new MyStringLog("animals");
This is supposed to construct a new MyStringLog object named animals, but I get 2 errors when I compile, both, that MyStringLog symbol is not found.
ArrayStringLog.java: http://pastebin.com/Z624DRcm
MyStringLog.java:http://pastebin.com/zaH2S3yg
TestDriver.java:
public class TestDriver {
public static void main(String[] args) {
MyStringLog animals = new MyStringLog("animals");
animals.insert("dog");
}
}

I got this to roughly work. To do so...
for the file ArrayStringLog.java, I removed the implements StringLogInterface because I simply don't have access to that interface. After doing so, I removed the package ch02.stringLogs;. When I compiled this file with the package... still there, I recieved the error: Exception in thread "main" java.lang.NoClassDefFoundError: ArrayStringLog (wrong name: ch02/stringLogs/ArrayStringLog)
Then in MyStringLog.java, I removed the import ch02.stringLogs.*;. I then saved and compiled the code, and ran the TestDriver, to which I received no compilation errors.
This leads me to believe that your error stems from the package statement in ArrayStringLog.java.
To finally get a compilation, I put all four files (ArrayStringLog, MyStringLog, StringLogInterface, TestDriver) into the same directory, removed any package... statements, added back implements StringLogInterface to ArrayStringLog.java, compiled each one, and then ran TestDriver with an added toString method from which the output was:
Log: animals
1. dog
2. cat
Here was the test driver:
public class TestDriver {
public static void main(String[] args) {
MyStringLog animals = new MyStringLog("animals");
animals.insert("dog");
animals.insert("cat");
System.out.println(animals.toString());
}
}
To make clear, ArrayStringLog begins with:
public class ArrayStringLog implements StringLogInterface

try adding import MyStringLog; to your file.
Also are you using packages? In which case it should be import <package_name>.MyStringLog;

Related

Under one package : compiler fails to read other class

I have three java files in one package : 'Receiver'.
CMReceiverMutant.java
CMReceiverMutantContext.java
TestDriver.java
Here is my TestDriver.java
package Receiver;
public class TestDriver{
public static void main (String[] args){
TestCase1();
// alternateTestCase1();
}
public static void TestCase1(){
CMReceiverMutant obj = new CMReceiverMutant();
obj.INT1SurvFlag();
obj.Exitw0();
System.out.println("Test case 1 reaches state :"+obj._fsm.getState().getName());
if(obj._fsm.getState().getName().equals("CMReceiverMap.Final"))
System.out.println("Test Case 1 passes!");
else
System.out.println("Test Case 1 fails");
}
}
I compiled TestDriver which depends on CMReceiverMutant.java. Eventhough I put them in the same directory. The compiler seems can't read CMReceiverMutant.java and it makes error :
TestDriver.java:11: error: cannot find symbol
CMReceiverMutant obj = new CMReceiverMutant();
^
symbol: class CMReceiverMutant
location: class TestDriver
I use cmd
javac -classpath Receiver\TestDriver.java
and I've tried
javac -classpath Receiver*.java
The errors are the same. Can you tell me whats the problem is?
Thank you
please check "Source" packages in the "Java Build Path" sometimes if the package isn't registered there the compiler fails to load them.
Hopefully it worked for me.

java: using an array of objects across files

OK so, im very new to java and the solution is probably simple so please bear with me, but basically i'm trying to make a film database using an array of a movie class. i have 3 .java files: the tester, the database, and the movie class. my problem is i'm really not sure how to make my tester file recognize the movies array from the database file, and every solution ive found has just given me more errors.
tester:
public class DatabaseTester extends MovieDatabase{
public static void main(String[] args) {
System.out.println(MovieDatabase.movies[1].getTitle());
}
}
the database:
public class MovieDatabase {
public static Movie movies[] = new Movie[2];
public static void movieDb(String[]args){
movies[1].setTitle("Test Title");
}
}
^the movie class has a set title method. i'm not too sure about the database's code in particular but it was the only way i could find that didn't give me errors. i'll post the full movie class if necessary but it's quite long so... only if needed
the error i get if i try to getTitle(); from the MovieDatabase:
Exception in thread "main" java.lang.NullPointerException
at DatabaseTester.main(DatabaseTester.java:35)
i'm aware this error is from the program thinking the array is not initialized, so it just must not be recognizing my database file... if i try to getTitle from the MovieDatabase, it simply doesn't recognize it, and will either give me an error or nothing. i cannot find a way to get around this aside from putting the Movie initialization in the main (which i have confirmed works, but it's not what i want to do).
You can try this the following changed code In the class DatabaseTester
public class DatabaseTester {
public static void main(String[] args) {
System.out.println(Database.movies[0].getTitle());
}
}

how to solve package does not exist error in java

I am trying to compile my java file name Test.java. Test.java calling a class com.api.APIUser.java which is available in a user.jar file. I have added user.jar in lib folder. But Test.java is unable to pick APIUser.java. While I am compiling Test.java using javac I am getting error
"package com.api does not exist".
Test.java
import com.api.APIUser;
public class Test{
APIUser ap = new APIUser();
ap .login();
public static void main(String[] args){
//to do
}
}
APIUser
package com.api
public class APIUser{
public string login(){
//to do
return string;
}
}
If any one have idea why I am getting this error.please suggest me solution.
Thanks in advance.
put a semicolon after the package com.api like as below
package com.api;
clean and build the project and run if any issue inform
You have multiple issues in your code.
You have no line termination present for the com.api import in the APIUser class;
You have a syntax error in your login method.
Below is the improved code:
import com.api.APIUser;
public class Test {
// APIUser ap = new APIUser(); // This call should be in the method body,
// there is no use to keep it at the class level
// ap.login(); // This call should be in method body
public static void main(String[] args) {
// TO DO
APIUser ap = new APIUser();
ap.login();
}
}
APIUser
package com.api; // added termination here
public class APIUser {
//access specifier should be public
public string login(){
//to do
//return string;//return some value from here, since string is not present this will lead to error
return "string";
}
}
Also be sure that the JAR file is present in the classpath. If you are not using any IDE, you must use the -cp switch along with the JAR file path, so that a class can be loaded from there.
You can use the code below to understand how to compile your class using classpath from command prompt.
javac -cp .;/lib/user.jar; -D com.api.Test.java

Java: multiple packages minimum working example

Trying to understand how Java packages work with classpath etc. In ~/java/tmp/test/HelloWorld I created HelloWorld.java:
package test;
import test2.Hello2;
public class HelloWorld {
public static void main(String[] args) {
Hello2 x = new Hello2();
x.blagh(args);
}
}
Then in ~/java/tmp/test2/Hello2 I created Hello2.java:
package test2;
public class Hello2
{
public static void blagh(String[] args) {
System.out.println("Hello, World");
}
}
Working in ~/java/tmp, I try to compile using:
javac -g test/HelloWorld/HelloWorld.java
I get the following errors:
test/HelloWorld/HelloWorld.java:3: package test2 does not exist
import test2.Hello2;
^
test/HelloWorld/HelloWorld.java:8: cannot find symbol
symbol : class Hello2
location: class test.HelloWorld
Hello2 x = new Hello2();
^
test/HelloWorld/HelloWorld.java:8: cannot find symbol
symbol : class Hello2
location: class test.HelloWorld
Hello2 x = new Hello2();
I've tried things like setting the classpath (to every possible combination of the above paths I could think of), changing which directory I run the compiler from, etc. Nothing works. Please help.
Your HelloWorld.java belongs to package test, so it should live in a directory named test, not test/HelloWorld. Same with Hello2.java, it should live in test2, not test2/Hello2. Move HelloWorld.java to the test directory and Hello2.java to the test2 directory and give it another try.

java.lang.ClassNotFoundException when instantiating an inner class

I have a following problem with an innner class. Here's the code:
public class PGZUserManagerBean {
// joomla login as separate thread
private class JoomlaLogin extends Thread {
private AuthJoomla authJoomla;
public JoomlaLogin(AuthJoomla authJoomla){
this.authJoomla = authJoomla;
}
#Override
public void run(){
this.authJoomla.authJoomla();
}
}
public void validateuser(){
AuthJoomla authJoomla = new AuthJoomla();
JoomlaLogin joomlaLogin = new JoomlaLogin(authJoomla);
joomlaLogin.start();
}
}
I'm getting java.lang.ClassNotFoundException: PGZUserManagerBean$JoomlaLogin on runtime. I'm using Java 1.6.
Thank you for the help in advance.
al
I strongly suspect that you've copied the class files from one place to another (or put them in a jar file) but you've failed to copy/include PGZUserManagerBean$JoomlaLogin.class.
Check where you're running the code, and look for the class file that the JVM can't find. It will definitely be in your compilation output.

Categories