This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&b selftest 10.6 packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&b selftest 10.6 packages" Watch "K&b selftest 10.6 packages" New topic
Author

K&b selftest 10.6 packages

Juan Handal
Greenhorn

Joined: Jan 25, 2005
Posts: 29
package client;
public class Stuff{
public static int doStuffi(int x){return (x++)*x; }
public static final int MY_CONSTANT=5;
}

import client.Stuff;
import static java.lang.System.out;

class User {

public static void main(String [] args){

new User().go();
}

void go(){out.println( doStuffi(MY_CONSTANT ) );}




}
bin
|
|---client

set in bin:
javac -cp . client\User.java
Do not compile(Don't find Stuff.java)
I'd appreciate your help.

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
First, you have some compile time problems in the declaration of your file User.java

You're a missing a bracket and you are trying to access a static members in another class without using the name of the class that holds the members.

You must declare your code like this:



Now, let's see about the javac command line.

If you have your source files in the current directory where you are running the javac command.

You must first compile the Suff.java file, like this

javac -d ./bin Stuff.java

The -d flag indicates where you want to put the compile class files.

Now, we can compile the User.java file. Like this

javac -cp ./bin -d ./bin User.java

In this case -cp flag is to indicate to the compile where it can find the Suff.class and the -d flag to indicate to the compile where to locate the locate class.

I hope this helps!
[ May 22, 2006: Message edited by: Edwin Dalorzo ]
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
This compiles and runs fine:

import client.Stuff;
import static client.Stuff.*;
import static java.lang.System.out;

class User {

public static void main(String [] args){

new User().go();
}

void go(){out.println( doStuffi(MY_CONSTANT) );}


In another file:
package client;
public class Stuff{
public static int doStuffi(int x){return (x++)*x; }
public static final int MY_CONSTANT=5;
}

From the parent directory of client (where I have User.class), I simply ran this as follows:

java User //no need to use -cp because I am in a folder that contains client folder
Juan Handal
Greenhorn

Joined: Jan 25, 2005
Posts: 29
Thanks Edwin great explanation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&b selftest 10.6 packages