Skip to main content

A simple problem about HelloWorld

5 replies [Last post]
jasoncoolmax
Offline
Joined: 2008-08-17
Points: 0

//Main.java
package Mypackage;
class Main
{
public static void main(String avgr[])
{
System.out.println("yes");
}
}

How to compile and run this program?
Note: the class Main is in the package Mypackage
Do I need to create a folder named 'Mypackage' ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
akjkr
Offline
Joined: 2008-09-03
Points: 0

Hi,
Create a folder with name of the package
Place all the java files written under this package here.
Now while compiling compile from a higher directory above the directory with package name.
for ex:if you create MyClass.java under myPack
create a folder myPack
put MyClass.java here
now come out of the directory and compile using
javac myPack/MyClass.java
if you compile in the the MyPack dir itself,compiler says File not found

sfshaza
Offline
Joined: 2004-06-03
Points: 0

Hmmm... I've been going through the package documentation here:
http://java.sun.com/docs/books/tutorial/java/package/index.html

and, I agree, it doesn't really give you a good example of how to compile a program that has been created package -- this is unfortunate since all Swing examples now use packages. Here is the current index for the Package lesson:

Creating and Using Packages:
Creating a Package
Naming a Package
Using Package Members
Managing Source and Class Files
Summary of Creating and Using Packages
Questions and Exercises: Creating and Using Packages

I wonder what you collectively think about adding a page after "Creating a Package" and before "Naming a Package" that shows a simple example just like what we have here?

Thoughts?

Sharon

jhipp117
Offline
Joined: 2008-08-24
Points: 0

Yes Sharon I completely agree.

I think the whole lesson set could be improved by interweaving with more worked examples.

In my thread about the life of variables I suggested that the "theory" section on classes and objects should be tied in with the "practical" section Introduction to Swing.

I also believe the "theory" section on packaging programs in jar files, could be tied in with practical examples in Swing/NetBeans.

jasoncoolmax
Offline
Joined: 2008-08-17
Points: 0

I mean compile and and run it in command line.

I tried:
java Main
and
java Mypackage.Main
but they doesn't work. Can somebody help me?

jero_x
Offline
Joined: 2007-03-19
Points: 0

u need javac.exe for compile (javac [ options ] [ sourcefiles ] [ @argfiles ])

more you can read at http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javac.html