Java: multiple packages minimum working example - java

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.

Related

Getting error while executing java programs of packages from command line

I'm executing the programs from command line and using packages in them.
my program file names are TestA.java and TestB.java.
I've executed below initially
javac TestA.java
No issues for the above and it generated the class file as well
for the following i'm observing the issue
javac TestB.java
output :
TestB.java:2: error: '.' expected
import TestA;
^
TestB.java:2: error: ';' expected
import TestA;
^
2 errors
and the TestA.java file is
package a.b;
class TestA {
public static void methodPublic(){
methodPrivate();
}
protected static void methodProtected(){
methodPrivate();
}
static void methodDefault(){
methodPrivate();
}
private static void methodPrivate(){}
}
TestB.java content is :
package a.b;
import TestA;
public class TestB {
public static void main(String args[]) {
TestA.methodPublic();
TestA.methodProtected();
TestA.methodDefault();
}
public static void methodPublic() {
}
protected static void methodProtected() {
}
static void methodDefault() {
}
private static void methodPrivate() {
}
}
I'm executing the javac by navigating to b folder where these two files exist.
I'm executing the javac by navigating to b folder where these two files exist.
You don't want to do that; the fully qualified class name of every class includes the package. They form a tree. Much like your filesystem. From the b folder move up two directories (to the folder containing a - e.g. cd ../.. or cd ..\.. on Windows). Then
javac -cp . a/b/TestA.java a/b/TestB.java
Also, you would normally want that to be written to a "binary" output folder. So
javac -cp . -d bin a/b/TestA.java a/b/TestB.java
Finally, you don't need to import TestA because it is in the same package as TestB. But, if you want to you need
import a.b.TestA;

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 compilation / package / namespace error

This is the stupidest, simplest problem ever with basic Java, but what am I doing wrong?
In a directory structure called
com/myname/robos
- Robo.java
- Arena.java
Robo.java :
package com.myname.robos;
public class Robo {
public void fala() {
System.out.println("Gleep Beep Boop!");
}
}
Arena.java :
package com.myname.robos;
import com.myname.robos.Robo;
public class Arena {
public static void main(String[] args) {
Robo r2 = new Robo();
r2.fala();
}
}
When I try to :
javac Robo.java
it compiles.
When I then try to
javac Arena.java
I get
Arena.java:3: error: cannot find symbol
import com.myname.robos.Robo;
^
symbol: class Robo
location: package com.myname.robos
Arena.java:8: error: cannot find symbol
Robo r2 = new Robo();
^
symbol: class Robo
location: class Arena
Arena.java:8: error: cannot find symbol
Robo r2 = new Robo();
^
symbol: class Robo
location: class Arena
3 errors
I KNOW it's an error about incompatibility between directory / package etc. names.
But I still never get this right. What should I be writing?
You need to ensure that your files are in the same structure as your package reference, say:
if your .java files are for instance at ../Desktop you must create folders with names: com, myname and robos and then paste them there, so your files will be inside ../Desktop/com/myname/robos.
Then you just need to compile Arena.java and run it:
$ javac com/myname/robos/Arena.java
$ java com/myname/robos/Arena
Positioning you inside ../Desktop

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 can't find symbol from .class in the same directory

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;

Categories