I have this java class :
package myClass;
public class myClass
{
private int herAge ;
public void setHerAge (int herAge)
{
this.herAge = herAge ;
}
}
and I want to import this class after compile it in another source file called Test.java exists in the same directory , and here what Test.java contains :
import myClass ;
public class Test
{
public static void main (String []args)
{
myClass Rudaina = new myClass();
Rudaina.setHerAge(30);
}
}
when I compile Test.java I see this in my console :
Test.java:1: error '.' expected
import myClass ;
^
Test.java:1: error '.' expected
import myClass ;
^
Your class myClass is also in package called myClass, which can be a cause of confusion.
Try:
import myClass.myClass;
This is called the fully qualified name of the class.
A package is meant to group classes which are conceptually related. Having a package named after a single class is not very useful.
You could name your package after the name of your project. For instance, you could name it
package assignment1;
And then import your class like this:
import assignment1.myClass;
While what everyone wrote is true, since the two files are in the same directory, no import should be necessary.
FYI, it is customary to capitalize the names of classes.
It's better to put the same package name or a different one as mention below on top of the Test.java file;
package myClass; //or some other name also viable
Then when you compile you can do like this;
javac -d . myClass.java
javac -d . Test.java
The -d specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows), etc. If you want to keep the package within the same directory, you can use the .(dot).
After that use the import statement inside Test.java like this;
import myClass.*;
or
import myClass.myclass;
After that when you run the Test class do like this;
java myClass.Test //myClass in here is package name if you use some different package name use that
You should use the whole class name - including the name of the package.
You are missing the package name. Try import myClass.myClass;
Related
Example:
I have a class called ProgA
package test;
public class ProgA
{
public static void main(String[] args)
{
ProgB pb = new ProgB();
pb.callMe();
}
}
Now I have the ProgB like below:
package test2;
public class ProgB
{
public void callMe()
{
System.out.println("inside callme");
}
}
After compiling ProgB.java its class file is generated in the test2 package. Now when I try to compile ProgA.java using this command:
javac -cp C:\Users\MyName\Desktop\test2 ProgA.java
I get the error that it cannot find ProgB.
My question is why cant java look inside the class path to find ProgB.class file and compile my ProgA.java successfully? The code works fine when I specify the fully qualified class name of ProgB inside ProgA.java code and run with the classpath set to -classpath C:\Users\MyName\Desktop. Why to have the fully quilified name when I am already specifying the full class path to find ProgB. I am not clear with that concept of classpath and fully qualified class name. Please explain me. Thank you
First you would need to import the class. This is why it asks you to use a fully qualified class name. You cannot use a class that is not in the same package without importing it (or using the fully qualified class name).
import test2.ProgB;
Then while compiling, you should provide the class path till the root location, the compiler will look for the class using the package name as the path.
Your compile command should be.
javac -cp C:\Users\MyName\Desktop ProgA.java
In order to use a class from another package, you need to either use the fully qualified class name, or have an import statement. This is a .java source code requirement. It can't be fixed simply by fiddling with the compiler's classpath.
Without an import statement, unqualified names are assumed to belong to the current source file's package. If you're in a package test file, the identifier ProgB will match test.ProgB but not test2.ProgB. The compiler won't search other packages unless you tell it to.
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.
I'm trying to declare a package in a file as follows:
import java.util.*;
package rtg;
public class Generate
{
// ...
}
But I'm getting an error when I try to compile this:
Generate.java:3: class, interface, or enum expected package rtg;
Why am I getting this error?
it should be
package rtg;
import java.util.*;
public class Generate{
}
In java you first define package then imports and then class. See wiki here: Java_package and Oracle's tutorial here: Java Packages
Edit
Now to call Genereate class from a class in same folder that is rtg folder:
package rtg;
public class GUI{
Generate gen = new Generate();
}
Make sure all words are spelled correctly.
The pacakge declaration must be the first thing in a Java file (apart from comments). You can't put the imports above it.
All the examples are above is good but we have to compile this package making class by swich standard ... You have to give "-d" and destinations folder for making package in it. "c: \f1 >javac -d e: \f2 temp . Java" 'c,e'are drive, 'f1,f2' are folder, temp is class name.