• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&b selftest 10.6 packages

 
Juan Handal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Edwin great explanation!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic