I have a directory structure like com/example/web under the root directory which contains a java file Bear.java. I have another java file BearExtra.java in directory structure com/example/model in same root directory as above. I am calling a method in BearExtra.java from Bear.java and I am getting the error that the package does not exist.
I have imported com.example.model package in my java file. Can give me some advice?
This works:
com/example/model/BearExtra.java
package com.example.model;
public class BearExtra {
public static void go() {
System.out.println("Yay, it works!");
}
}
com/example/web/Bear.java
package com.example.web;
import com.example.model.*;
public class Bear {
public static void main(String[] args) {
BearExtra.go();
}
}
Now, to compile and run these classes, go to the directory where you can "see" the com folder and do:
*nix/MacOS
javac -cp . com/example/model/*.java com/example/web/*.java
java -cp . com.example.web.Bear
Windows
javac -cp . com\example\model\*.java com\example\web\*.java
java -cp . com.example.web.Bear
and the following is being printed to the console:
Yay, it works!
Did you specify the 'package' in your class files?
Bear.java
package com.example.web;
import com.example.model.*;
class Bear { ...
BearExtra.java
package com.example.model;
public class BearExtra { ...
Related
Here My first file code
package com.shubham.packages.a;
import static com.shubham.packages.b.Message.Hello;
public class Greeting {
public static void main(String[] args) {
System.out.println("Hello World");
Hello();
}
}
Here my second file code
package com.shubham.packages.b;
public class Message {
public static void main(String[] args) {
}
public static void Hello() {
System.out.println("This is Awesome.");
}
}
Here the error I got when I run the program.
When you compile your code javac needs to know where to look for all of your source/class files. You could go to "java_tutorial" folder and run.
javac com/shubham/packages/a/Greeting.java com/shubham/packages/b/Message.java
That should compile both of your java files into class files at the same location. Then you should be able to run.
java com.shubham.packages.a.Greeting
I might be using the wrong slashes for windows
You can explicitly name all of the necessary java files, so this should cause both Greeting.java and Message.java to be compiled in place.
When you run java, the CWD is on the classpath by default so that means the package com.shubham.packages.a and ...b should be on the classpath in their correct location.
A slightly better way to do this is to create a folder called "build" or whatever you like.
javac -d build com/shubham/packages/a/Greeting.java com/shubham/packages/b/Message.java
That will output the class files to the build folder. Then when you run it.
java -cp build com.shubham.packages.a.Greeting
I am making Java files in my package programapper, and have package programapper; at the top of every file. One of the files is called TestMain.java:
package programapper;
import java.util.*;
import java.util.List.*;
import java.lang.*;
import java.io.*;
class TestMain {
public static void main(String args[]) {
String filename = null;
System.out.print("File to read: ");
Scanner in = new Scanner(System.in);
}
}
Whenever I compile, I run
javac -d . *.java
This compiles fine and I am left with all of my .java files in my primary Programapper/ directory, and then I have Programapper/programapper, where all of my class files are stored.
I am unable to then load the class and run its main function. I'm using
java -classpath .:programapper/ programapper.TestMain
and I have also tried using
java -classpath .:programapper/ TestMain
The error I am receiving is Error: Could not find or load main class programapper.TestMain
You need to make the class public:
public class TestMain {
...
If you omit visibility, the class will be accessible only from the classes within the same package. Which is not sufficient for the main class.
Additionally, instead of specifying just the class name, you need to specify classpath as "." and specify the full class including package:
java -classpath . programapper.TestMain
I have two classes like:
Class GetData is like:
package com
public class GetData{
private String name = "John";
public String getName(){
return name;
}
}
package com.test;
import com.GetData;
public class Test{
public static void main(String args[])
{
GetData data = new GetData();
System.out.println("Welcome "+data.getName());
}
}
When I'm trying compile the Test class it throws an error like undefined symbol for GetData. I tried like:
javac -cp .com/GetData.class Test.java
Kindly help me out
The problem is with how you're specifying the classpath. You just need to specify the directory which is at the root of the directory containing the classes, so if you've got com/GetData.class you just need:
javac -cp . Test.java
Although you should really have the source code in a directory hierarchy matching the packages, so I'd expect
javac -cp . com/test/Test.java
Then run with:
javac -cp . com.test.Test
All of this is assuming that . isn't on your classpath - it is by default, so you may well find you can just use:
javac com/test/Test.java
java com.test.Test
Its the problem how you are running the code. Simply you can run with eclipse Run As->Java Application if you are not sure how to run through command console.
I have the following directory structure:
A.java
a
โโโ A.java
A.java:
import a.*;
class A {
public static void main(String[] args) {
System.out.println("Hello Human");
}
}
a/A.java:
package a;
public class A {}
And it compiles and runs:
$ javac A.java && java A
Hello Human
But when I remove a/A.java (but keep the folder a), I can no longer compile it:
$ rm a/A.java
$ javac A.java && java A
A.java:1: package a does not exist
import a.*;
^
1 error
Why?
You might say that the package a is not observable:
A package is observable if and only if either:
A compilation unit containing a declaration of the package is observable (ยง7.3).
A subpackage of the package is observable.
source: Observability of a Package
This is your problem right here:
import a.*;
It's looking for something that doesn't exist, since that package is no longer there.
Remove that line from A.java and it should work fine.
The reason it can't find the package is because there is no compilable classes within the folder, and so it isn't recognised as a java package.
One option for creating an empty package, if you do need one, would be to specify a package-info.java file, which is generally used as a javadoc for packages.
When preparing for the SCJP exam, we were going through the following code:
package certificaton;
public class OtherClass
{
public void testIt()
{
System.out.println("otherclass");
}
}
And this:
package somethingElse;
import certification.OtherClass;
public class AccessClass
{
public static void main( String args[])
{
OtherClass o= new OtherClass();
o.testIt();
}
}
I placed both the above files in the following directory: C:\scjp\temp8 ; and the strange thing is that, the .java files are compiling and results in two .class files being created in the same directory. The thing I want to ask, is that, the difference between packages and directory. Isn't it true that the class files could be created in a directory other than the one stated in the package declaration? And the package declaration is something 'virtual', and disregards the windows directory structure. In addition, isn't it also true that, by executing the following command:
javac -d . OtherClass.java
The directories are created conforming to the package declaration, which isn't always mandatory?
The directories are created conforming
to the package declaration, which
isn't always mandatory?
No, the package and directory structures must match. It's mandatory, not optional.