import packagename.* not working - java

I have written the following code:
package abc.def;
public class test {
public void test() {}
public void disp() {
System.out.println("in disp");
}
}
then I used following command to compile:
javac -d . test.java
it works fine, but when I tried to import the class "test" using "import abc.def.*" it does not import test class, the code is :
import abc.def.*;
public class checktest {
public static void main(String a[]) {
test t = new test();
}
}
following error is generated:
D:\javaprograms>javac checktest.java
checktest.java:8: cannot access test
bad class file: .\test.java
file does not contain class test
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
test t = new test();
^
1 error

I also had the same problem.
No additional classpath is required to set.
According to your scenario, your working directory might contains test.java file. You can just remove the test.java file from the working directory and compile using javac checktest.java.
It will work.
Thanks.
Britto

Did you make the proper directory structure? You need to have the test.java file in abc/def if that's the package name you want.
You can also point to the compiled test.class file with -cp flag
Example:
javac -cp test checktest

Your directory structure should look like this:
current working directory
checktest.java
abc
def
test.java
Then, from the directory on the top, you can compile checktest:
javac checktest.java
This will automatically find (and compile) test.java too. If you only want to compile test, do it this way:
javac abc/def/test.java
Then all the class files will be in the right directories, too.

It seems that you have by mistake compiled test.java in the topmost directory itself, therefore the JVM is picking test.class from the top most directory and also from abc\def\test.class hence conflict is happening.
please type: ls test* in the top most directory and confirm if that is the case and delete this extra test.class and then recompile.

first know this - To use the package in other programs, compile the .java files as usual and then move the resulting .class files into the appropriate subdirectory of one of the directories referenced in your CLASSPATH environment variable.
For instance if /home/name/classes is in your CLASSPATH and your package is called package1, then you would make a directory called package1 in /home/name/classes and then put all the .class files in the package in /home/name/classes/package1.
Now suppose your classpath is /home/name/classes then compile
package abc.def;
public class test {
public void test() {} public void disp() { System.out.println("in disp"); }
}
using $ javac -d /home/name/classes test.java
Now put this code
import abc.def.*;
public class checktest {
public static void main(String a[]) {
test t = new test();
}
}
inside the folder

Related

Java | Custom Created Package Does not Exist

Purpose
I want to be able to create a package and call it.
Alternatively, I would like to create separate files for my method (to avoid having x classes in one file).
Setup
Here is my LetterGrader.java file:
package grade.util;
import java.util.*;
import java.io.*;
public class LetterGrader {
private void readArgs() {
System.out.println("Hello, read CLA!");
}
}
Here is my TestLetterGrader.java file:
import java.util.*;
import java.io.*;
public class TestLetterGrader {
public static void main(String[] args) {
LetterGrader letterGrader = new LetterGrader(); // instantiate
letterGrader.readArgs(); // call method
}
}
Steps Taken
First, I compile LetterGrader:
This auto creates the bin/grade/util/LetterGrader.class file
javac -d bin -sourcepath src src/grade/util/LetterGrader.java
Here is my working directory at this point
Second, I compile TestLetterGrader:
This fails
javac -d bin -sourcepath src src/grade/util/TestLetterGrader.java
The error message:
src/grade/util/TestLetterGrader.java:6: error: cannot find symbol
LetterGrader letterGrader = new LetterGrader(); // instantiate
^
symbol: class LetterGrader
location: class TestLetterGrader
Question
I believe I am misunderstanding how to call a classes from separate files (in the same location). How can I accomplish this?
You are importing class that is in bin folder. Don’t do that it would not work. You don’t need any import, because the classes are in the same place. Make package under src folder and place the classes there. Remove package grade.util and rename it to the package where you place the classes.
File structure:
src
\
\
yourpackage
\
\
LetterGrader.java TestLetterGrader.java
Then delete everything in your build folder and compile the classes. Java will make it’s magic. You do need to worry about bin folder, it is only for storing compiled classes.
Classes will look like this:
//package name that you created
package yourpackage;
public class LetterGrader {
//need to be public when calling from another class
public void readArgs() {
System.out.println("Hello, read CLA!");
}
}
And
//folder that you placed the .java files
package yourpackage;
//without any import
public class TestLetterGrader {
public static void main(String[] args) {
LetterGrader letterGrader = new LetterGrader(); // instantiate
letterGrader.readArgs(); // call method
}
}
Your second question:
You can use classes from other folders, but you you have to import them and they have to be under src folder.
Tell you have class A.java in folder Second and class B.java in folder Main. You will import the the folder in this case import Second.A;
And then call the class A a = new A();
When you have method in a that you want to call simply do:
a.yourmethod();
You have to change private void ... to public void... because you cannot call private outside of the class.
When you are running compiled classes they have to be in the same folder.
Thanks #maratonec for the guidance.
My initial mistake was that I was misunderstanding/misassigning the classpath assignment variable when running a program via terminal. The below helped me.
Compiling and Running a Java Program (on a PC)
• Set the working directory (say, JavaBook)
C:\> cd JavaBook
• Compile HelloWorld.java
C:\JavaBook> javac -d bin src\HelloWorld.java
•Run the program
C:\JavaBook> java -classpath bin HelloWorld
Also, the approach of having all my class files in the same location simplified things. I didn't have to worry about classpath. But not ideal as I have many files to work with.
As for the package creation, I am going to play around with java a bit more before using it. I think I need to solidify my understanding.
Thanks for helping me!

Trouble running java app from cmd

I have two files, app.java and test.java
They both reside in the same package, and they compile just fine with "javac app.java test.java"
Two class files are then created.
However, when I go to run them with the command "java app" because app has the main method, I get "Error: Could not find or load main class app"
app.java:
package working_directory;
public class app {
public app() {
}
public static void main(String [] args) {
test testing = new test();
System.out.println(testing.calculate(60));
}
}
This Is the test.java
package working_directory;
public class test {
public test() {
}
public int calculate(int x) {
return (int) x * x * x;
}
}
Make sure to choose the right path for compilation and running code:
D:\
+--Folder(start cmd here)
+---working_directory
+----app.java
+----test.java
How to compile
D:\Folder\>javac working_directory\*.java
How to run
D:\Folder\>java working_directory.app
To use the java command, you must specify the fully qualified name of the class you want run. This means that you need to specify the package name as well.
You should run this:
java working_directory.app
Since working_directory is the package name.
You must provide a classpath, when running it from command line:
(for windows)
java -classpath . app
You have a package name declared in other words that's a folder. Your project should look like this then
C:\YourProject
C:\YourProject\working_directory
C:\YourProject\working_directory\app.java
Your Project starts at root level so it's C:\YourProject there you have to use the command line and type java working_directory.app

Adding -classpath from command line

So I have two java files, Print.java and StaticImport.java, in src/com/test.
StaticImport.java:
package com.test;
import static com.test.*;
class StaticImport {
public static void main(String[] args) {
System.out.println("Hello world");
Print.print("This is cool");
}
}
Print.java:
package com.test;
public class Print {
public static void Print(String command) {
System.out.println(command);
}
}
So basically there is the StaticImport class that uses Print class.
How can I compile the StaticImport with javac in command line?
I have tried for example: javac -cp /home/pathToProj/ StaticImport.java, but with no success.
In java, the classpath contains class files, not java code.
First, you need to compile Print.java, since you need it to be on your classpath. Then you need to set the classpath for the compilation of StaticImport to be the directory containing the "com" directory above Print.class.
You can also compile both files at the same time, using a single call to javac.
However, the best thing to do is to use either maven or gradle to build it for you. They look after your classpath, and do a million other things besides.

How to compile and run a java class with a package

I have a class called MyClass in the file MyClass.java file (code mentioned below)
package myclass;
class MyClass {
public int add (int a, int b){
return a+b;
}
public static void main(String args[]) {
MyClass obj = new MyClass();
System.out.println(oobj.add(2, 3));
}
}
I am compiling the class with
javac MyClass.java
But I am trying to run the class using
java MyClass
or
java myclass.MyClass
I am getting the Error
Error: Could not find or load main class MyClass
But, I am able to run this program if I omit out the package name.
where am I going wrong?
Make sure that you are inside the parent directory of the package folder (the folder in which your compiled class file is), and execute the following command:
java myclass.MyClass
Below is an example file structure:
bin
-> myclass
-> MyClass.class
In the example structure above, you would have to execute the command from the "bin" directory.
Also, define the class as public and recompile the java source file.
I ran into this too. It's very frustrating for someone from other languages. The key here is, the java file has to be in the right directory depending on the package declaration.
if the java file Test1.java starts with
package com.xyz.tests;
Then the java file Test1.java needs to be in directory com/xyz/tests
You can compile and run as
javac com/xyz/tests/Test1.java
java com/xyz/tests/Test1
Good luck.
You Need To Compile The Class using :
javac -d ./myclass
I get my example to run by
java <package>.<class>
From parent directory of package

java packages: cannot find symbol

I'm having a strange error. I have 2 classes in the same package but they can't find each other. From what I remember, as long as the classes are in the same package, they should be able to call each other's methods.
My code looks similar to this:
in A.java:
package com.mypackage;
public class A{
public static int read(){
//some code
}
}
in B.java:
package com.mypackage;
public class B{
public static void main(String args[]){
int x = A.read();
}
}
and it's giving me a cannot find symbol variable A error.
Both of these classes depend on some .jar files, but I've already included the path of those jars to CLASSPATH and A.java compiled fine, but B can't find A for some reasons...
When I remove the package com.mypackage; in both classes then they compile fine.
Since you're compiling Java files that are in distinct packages, you'll have to ensure that they compile to the appropriate directories.
You can use this invocation to do just that. Substitute $SRC with the location of your source files, and you can let $BIN be the current directory, or some other location on your machine.
javac -sourcepath $SRC -d $BIN A.java B.java
When you want to run them, you have to add them manually to the classpath again (but that's not such a bad thing).
java -cp $BIN com.mypackage.B
This invocation should work; just made sure of it with A.java and B.java residing on my desktop. With the -d flag, that ensured that when they compiled, they went to the appropriate package folder scheme.
It should be:
A.java
package com.mypackage;
class A {
public static int read(){
//some code
}
}
B.java
package com.mypackage;
class B {
public static void main(String args[]){
int x = A.read();
}
}

Categories

Resources