File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to run example code Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "How to run example code" Watch "How to run example code" New topic
Author

How to run example code

Tim Mackey
Greenhorn

Joined: Mar 18, 2010
Posts: 6
I'm using S&B's SCJP Java 6 Study Guide. I'm through Chapter 2 at this point.
I can't figure out how to compile and run any of the example code without putting a wrapper around it, like:


I've tried NetBeans 6.5.1 and also from the Windows CMD command line.
I've spent 4+ hours looking through Google searches and also at this website (search returns 1000+ hits on "how to run")
Can anyone help? Thanks.

This message was edited 1 time. Last update was at by Ankit Garg

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

1. If the question consists of a method declaration as a part of description then create a public class with "public static void main(String[] s)" method and place the method in that class. Now you can test the method by calling it from main method. (First you have to create an object of the class and then have to call the method using that object, if the method is not a static method, )

2. If question is having statements as a pert of description then you can place the statements in main method and run the program.

If you can post a sample question then we can assist you more comfortably.
Tim Mackey
Greenhorn

Joined: Mar 18, 2010
Posts: 6
I apologize for not posting all the code. (I though everyone had access to the SCJP Study Guide)

Below is a typical example of the code I want to run - in file "House.java" (p. 178 of the Study Guide):


To run the above code I have to create a separate file "Main.java":


My question: is there some other technique to execute "House.main()" without a separate "Main.java" file?

Thanks.

This message was edited 2 times. Last update was at by Ankit Garg

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 735
Why you want to run it this way? When you can run the above mentioned classes straight from House main() method.
Any specific objective you want to run this program this way?

Please elaborate.

Hope then well directed assistance can be provided, or may be i am just missing some point.

Best Regards,

Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Tim Mackey
Greenhorn

Joined: Mar 18, 2010
Posts: 6
I would really like to run the above mentioned classes straight from House main() method.

Hence my question: is there some other technique (OTHER THAN THE ONE I POSTED) to execute "House.main()" without a separate "Main.java" file?

How is the main() method in House called from the command line? How do I setup NetBeans? Please educate me.
Please remember I'm a Greenhorn and a student. I'm looking for some simple instruction of the ilk: "At the comand line type <whatever>. Then type <whatever else>." I looking for a detailed cookbook solution.

Thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13404

Tim Mackey wrote:I would really like to run the above mentioned classes straight from House main() method.

Hence my question: is there some other technique (OTHER THAN THE ONE I POSTED) to execute "House.main()" without a separate "Main.java" file?

How is the main() method in House called from the command line? How do I setup NetBeans? Please educate me.


Running the main() method of the House class is done in exactly the same way as running the main() method of the Main class -- except you use House instead of Main.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Devaka Cooray
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2586

Tim, please UseCodeTags when you post a code. It's unnecessarily hard to read the code otherwise. Please edit your post to add code tags by clicking the button.


Author of ExamLab - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 735
Yes, please use code tags before posting a question.

Additionally, i will suggest you get a hold of a good book like Head First Java for the basics of Java.

More specifically, if you are using net-beans and you want to educate yourself for net beans working, i suppose
tutorial like this over net beans
will make you up and running.

Best Regards,
Tim Mackey
Greenhorn

Joined: Mar 18, 2010
Posts: 6
Here is what happens when I run my code from the command line:

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src>java Main
b h hn x
C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src>

This is what I would expect. Following Henry Wong's instructions, this is what happens:

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\s
t12>dir
Volume in drive C has no label.
Volume Serial Number is C839-2DA5

Directory of C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\sc
jp\Chapter_2\st12

03/21/2010 11:25 AM <DIR> .
03/21/2010 11:25 AM <DIR> ..
03/21/2010 11:19 AM 695 House.class
03/18/2010 11:02 AM 534 House.java
2 File(s) 1,229 bytes
2 Dir(s) 41,902,915,584 bytes free

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\s
t12>javac House.java

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\s
t12>java House
Exception in thread "main" java.lang.NoClassDefFoundError: House (wrong name: sc
jp/Chapter_2/st12/House)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: House. Program will exit.

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\s
t12>

It's not "exactly the same", Henry.
Has anyone actually tried my sample code on a Windows OS system? If so, please post your output.
If anyone can tell me what to type on the command line I would be very grateful.

And, yes, I will use [CODE] tags in the future. Thanks for letting me know.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13404

Tim Mackey wrote:

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\st12>javac House.java


Try....

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src> javac scjp\Chapter_2\st12\House.java


Tim Mackey wrote:
C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\st12>java House

Exception in thread "main" java.lang.NoClassDefFoundError: House (wrong name: scjp/Chapter_2/st12/House)
at java.lang.ClassLoader.defineClass1(Native Method)


C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src> java scjp.Chapter_2.st12.House

Henry

This message was edited 1 time. Last update was at by Henry Wong

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 13404

Tim Mackey wrote:
It's not "exactly the same", Henry.
Has anyone actually tried my sample code on a Windows OS system? If so, please post your output.
If anyone can tell me what to type on the command line I would be very grateful.


The difference is House is in a package and Main is not. And hence, behave slightly differently -- in that you don't have to mention it for Main.

Java requires the fully qualified name, so specify it.

Henry
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Tim,

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\s
t12>javac House.java

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\s
t12>java House


instead of executing it with "java House" just try it with "java -classpath . House" . Please let me know if you are able to execute it or not this way.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 11223

bhanu chowdary wrote:instead of executing it with "java House" just try it with "java -classpath . House" . Please let me know if you are able to execute it or not this way.

That's not going to work. The problem is that class House is in a specific package. You have to add the base directory of the package to the classpath and then mention the fully qualified name of the class on the command line when you run it.

Try this instead:

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src> javac scjp\Chapter_2\st12\House.java

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src> java scjp.Chapter_2.st12.House

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Jesper Young wrote:
bhanu chowdary wrote:instead of executing it with "java House" just try it with "java -classpath . House" . Please let me know if you are able to execute it or not this way.

That's not going to work. The problem is that class House is in a specific package. You have to add the base directory of the package to the classpath and then mention the fully qualified name of the class on the command line when you run it.

Try this instead:

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src> javac scjp\Chapter_2\st12\House.java

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src> java scjp.Chapter_2.st12.House


But since he is trying it from the directory where the .class file is located I thought "java -classpath . House" would work as this would search for the class file in the present directory. I checked it in my machine its working for me
Tim Mackey
Greenhorn

Joined: Mar 18, 2010
Posts: 6
Greetings all. I have found the solutions.

When running from the Command Line, remove the package statement from House.java

//package scjp.Chapter_2.st12;

Then I get the following output:

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\st12>javac House.java

C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\st12>java House
b h hn x
C:\CIW\Java_Prg_Fnd_Student_CD\Lab Files\Student Files\scjp\src\scjp\Chapter_2\st12>

In NetBeans, the package statement is required. Right-click on the project (scjp) and select "Properties", select the Run Category. Set "Main Class" to "scjp.Chapter_2.st12.House". Press F7 to step through the House class starting with the "main()" function.

I'm astonished and disillusioned at how few contributors took the initiative to actually compile the code on their system and run it. For those of you who did, thank you.
 
 
subject: How to run example code
 
MyEclipse, The Clear Choice