Suppose i have a directory "Animal" (without quotes) and i have java classes in it.How to add that directory to class path in Linux ? I mean suppose i want to use class files in "Animal" directory from some other folder , then i need to add "Animal" to class path .How to do it ?
Also suppose Animal directory has several sub directories and each of the sub directories also have directory having java class files .Then how to add each of them to java package ?
edit : Suppose i have two folders in Ubuntu say Downloads and Documents .Now in Downloads i created a folder called "animal" having a class named Dog.java . Now in Documents folder i created main method in which i try to make an instance of "Dog" class .I want to do it by importing the package "animal" in Download folder. How to do it ?We know that if we want to use some pre-built package in java then we import that package to use classes inside that package .Similarly suppose i want to import my own package , what i need to do ? When i write import and compile then it says package "animal" does not exist .So what i do so that it doesn't gives compilation error .Can some one elaborate by giving an answer .
I did lot of work before asking this question and when i was unsuccessful i finally asked this question .
To work this out in detail, with some code- the folder structure is:
~ (user folder)
~/Documents
~/Documents/useanimal
~/Documents/useanimal/UseDog.java
~/Downloads
~/Downloads/animal
~/Downloads/animal/Dog.java
The code for the two classes is below. To compile:
cd ~/Downloads
javac animal/Dog.java
cd ~/Documents
javac -cp ~/Downloads useanimal/UseDog.java
To run:
cd ~
java -cp Documents:Downloads useanimal.UseDog
and it will output
UseDog:main
Dog created
Code:
animal/Dog.java
package animal;
public class Dog {
public Dog() {
System.out.println("Dog created");
}
}
useanimal/UseDog.java
package useanimal;
import animal.Dog;
public class UseDog {
public static void main(String[] args) {
System.out.println("UseDog:main");
Dog dog = new Dog();
}
}
Related
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!
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
I'm learning java packages in school and I'm able to create and use my package on netbeans perfectly but cannot do it from the lubuntu command line. I get an error: could not find or load main class. Here is the code but I know this is not the problem since it works perfectly in netbeans
package animals;
public class MammalInt implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}
public void travel()
{
System.out.println("Mammal travels");
}
public static void main(String args[])
{
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
package animals;
interface Animal
{
public void eat();
public void travel();
}
I first compile Animal.java and put the Animal.class file into a directory
animals. I then compile MammalInt.java. If I do not put the Animal.class file in a animals directory it will not compile MammalInt.java . After I have both class files into the animals directory I do java animals/MammalInt and get the error: cannot find or load main class. I also have doe java MammalInt and get the same error. This is really frustrating. Please help.
When compiling (a set of files) you need to use the path.So use /
javac animals/*.java
When running the Java class, you need to specify the Java name of the class.
In your case this is done as follows:
java animals.MammalInt
This says you want the class MammalInt in the package animal. Depending on your installation you also need to add your current directory to your classpath (this is where java looks for .class files), resulting in:
java -cp . animals.MammalInt
Note that you run all commands from the root of your source code tree. This means the directory that contains the directories for your packages. So if you have the following direcotries:
project/
project/animals/
project/animals/Animal.java
Then run the commands from the project/ directories.
I am trying to follow this basic guide:
http://www.tutorialspoint.com/java/java_packages.htm
When I compile the file MammalInt.java I have an error:
MammalInt.java:4: error: cannot find symbol
public class MammalInt implements Animal{
^
symbol: class Animal
1 error
Both the files Animal.java and MammalInt.java are in the same directory. I have already compiled Animal.java.
Please help me!
You should:
Make sure the filenames are correct (Animal.java and MammalInt.java - the same as the class names with .java file extension)
Make sure they are both in a directory called animals (same as the package name)
Make sure they both have package animals; at the top of the files
Compile tham at the same time with the command javac Animal.java MammalInt.java
That works.
Classes have two access modifier: public and default.If you are making a class default that will be access-able from all other packages but if you are not specifying any access modifier i.e if you are giving default modifier that means that class will be access-able from the package where this class belong to.
Add on top of both files this:
package animals;
it should fix the problem.
How can one create a package in Java:
In a book i read its :
package package_name
public class whatever{}
.
.
.
But shouldn't this be enclosed in parenthesis such as :
package package_name
{
public class whatever{}
.
.
.
}
Just a minor confusion. Can anyone give me an example of the correct syntax?
The syntax for creating package
package package_name;
public class Whatever {
}
You can find more useful info Here.
When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.
Reference
Example :
package illustration; <------------
import java.awt.*;
public class Drawing {
. . .
}
Package is created as follows
package package_Name;
This package name has to be the first statement in the file.Once you declare package name start defining methods or classes or interfaces in it.
If this package you have to use in your any java file then write
import package_Name;
so that all the methods defined in the package will be accessible in java file.
No parenthesies. Package is nothing else then just a folder or set of folders. For example, if you create package named: utils, new folder will be created in your source folder. If you create package named org.utils, two folders will be created in your source folder. org and utils which will be inside of org folder. Also, every next package which starts with org. (for example org.ui) will be just a new folder created in org folder. So your folder hierarchy will look like this:
org--
|
utils
|
ui