• Post Reply Bookmark Topic Watch Topic
  • New Topic

package  RSS feed

 
Manisha Dudeja
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!!!
I am new to java, learning programming in java but having a problem in creating a package for my java class using command prompt and notepad.
I written a simple java program like:

package pack;

class Demo
{

public static void main(String a[])
{

System.out.println("I am in Package pack");
}
}

It is compiled but .class file created isn't in package, but with in the default folder
and that's why showing eror:
C:\new>java pack.Demo
Error: Could not find or load main class pack.Demo

please help me with it..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Your directory structure must match your package structure. So, when you create a class Demo in package pack, you'll need to save the source file Demo.java in a directory named pack: C:\new\pack\Demo.java

Then you can compile and run it from the commend line, with C:\new as the current directory:

C:\new> javac pack\Demo.java
C:\new> java pack.Demo
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

To compile the source you use the "javac" command. To run the program you use "java" command.

Also for "packaged" source, your source files need to be in a folder (package). So in your example: "javac pack/Demo.java" and to run it "java pack.Demo". Assuming you are in the C:\new folder.
 
Manisha Dudeja
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok sir, you mean that we need to create a package for .class file, it isn't created while compiling. But I was told by my teacher that it is created by compiler itself, and when I tried doing the a code earlier it worked(it created package for .class file itself), but now it isn't working.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your teacher is not wrong. But there is little change in the command.

javac -d <folder under which the package/package folder should be created> yoursourcefilename

for e.g. if you wish to create the package under the current working directory the command should be

javac -d . myfile.java
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default (with the -d flag) the class file goes in the same directory as your source.
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means d flag makes no difference, right? Then whats the use?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion, actually my previous is wrong.

WITHOUT -d flag the class files goes in the source folder. WITH -d flag class files can go any where you specify normally somewhere outside the source folder.
 
Manisha Dudeja
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I got it, now my codes are working.
Thank you very much. Thank you all for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!