Hi

I'm new to Java and I was dealing with the creation of packages.
Suppose I have the following two source files which I want to put in one package called mypackage.
The two source files are in the (Linux) directory: /home/user/workspace/mypackage.

package mypackage;
public class HelloWorld 
{
  	public static void main(String[] args) 
	{
    		System.out.println("Hello, world!");
                output.func();
 	}
}
package mypackage;
public class output 
{
  	public static void func() 
	{
    		System.out.println("This is my package!");
 	}
}

To compile the Helloworld class in Linux terminal, I did the following command:
javac mypackage.HelloWorld.java
but unfortunately I get the "javac: file not found: mypackage.HelloWorld.java" error.

Can you please help me with a step by step way of compiling and executing these source files.

cheers

mypackage/HelloWorld.java

performed from

/home/user/workspace

It is a bit confusing at first, but when you reference source files (.java) in packages, you use slashes, and think of the packages like directories or folders (which they are). When you are dealing with class files, you use dots instead of slashes.

Hi
Thank you very much!

Hi
Thank you masijade, it worked. But then in which cases will I have to use mypackage.HelloWorld.java?

You won't. You'll use

java -cp . mypackage.HelloWorld

when you execute.

because as I was reading I heard that I can compile the sourcefiles regardless of been in a certain directory, I heard of something like classpath setting which gave me a bit of confussion.

That "-cp" is the "classpath thing you've heard of and has to do with where the classes are not where the source files are.

Hi
Thank you.

complile by typing

javac -d . classname.java

then run using

java home.user.workspace.mypackage

first install jdk in linux system and set the path .
search in net the process of install jdk in linux and set path in the home directory

then compile using

javac -d . HelloWorld.java

then run using

java home.user.workspace.mypackage.HelloWorld

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.