This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compiling problem for Scjp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compiling problem for Scjp" Watch "Compiling problem for Scjp" New topic
Author

Compiling problem for Scjp

Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
My directory structure is like that-


In the x directory:


In the test directory:


In the myApp directory:

The question is :
If the current directory is x, which invocations will produce the output "test/Baz"? (Choose
all that apply.)

I know how to compile and run
let's say:
here is my answer :
c:\Users\Ian Su\Desktop\x>javac -cp test FindBaz.java //to compile
c:\Users\Ian Su\Desktop\x>javac -cp test;. FindBaz //to run to have the correct output

The answer is ok.

It is still ok!Then I tried to change the FindBaz.java.That is I inserted one line.
FindBaz.java after changing it.

In the x directory:

Other files are the same as before.
After changing it,I tried several ways to compile FindBaz.java.But I still can't compile.
Is there anybody able to help me with it!Thank you for your reading my post!
I know what the problem is.But what i want to know is ,can I compile in this latest situation.
help me out please.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Welcome to JavaRanch.

Why did you post this in the "Meaningless Drivel" forum? I'll move it to a more appropriate forum for you.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Thank you Jesper Young.Actually I am a starter in this forum.So I don't know where to put.I just want to post it.
Can I get a forum name like "Programmer Certification(SCJP)"?I mean can i edit that name or can I have a new name?
Thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Ian Su wrote:
Then I tried to change the FindBaz.java.That is I inserted one line.
FindBaz.java after changing it.

In the x directory:
1. package x; //I inserted this line
2. public class FindBaz {
3. public static void main(String[] args) { new Baz(); }
4. }
Other files are the same as before.
After changing it,I tried several ways to compile FindBaz.java.But I still can't compile.


c:\Users\Ian Su\Desktop\x> cd ..
c:\Users\Ian Su\Desktop>javac -cp x\test;. FindBaz.java
c:\Users\Ian Su\Desktop>java -cp x\test;. FindBaz


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Henry Wong wrote:
Ian Su wrote:
Then I tried to change the FindBaz.java.That is I inserted one line.
FindBaz.java after changing it.

In the x directory:
1. package x; //I inserted this line
2. public class FindBaz {
3. public static void main(String[] args) { new Baz(); }
4. }
Other files are the same as before.
After changing it,I tried several ways to compile FindBaz.java.But I still can't compile.


c:\Users\Ian Su\Desktop\x> cd ..
c:\Users\Ian Su\Desktop>javac -cp x\test;. FindBaz.java
c:\Users\Ian Su\Desktop>java -cp x\test;. FindBaz



Thank you Henry Wong for your advice!really!
I still can't compile FindBaz.java.I've already tried it but can't.Three files are in the same directory tree and one file is included with the package import(in this case,package x; in the FindBaz.java) and the other two don't have the package import.I don't think the problem starting from there!
help me out please.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Ian Su wrote:
I still can't compile FindBaz.java.I've already tried it but can't.


Without anymore information, we can't give you any help -- if would help if you cut-n-paste the exact error messages, when you ran the commands that you "already tried".

Henry
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Henry Wong wrote:
Ian Su wrote:
I still can't compile FindBaz.java.I've already tried it but can't.


Without anymore information, we can't give you any help -- if would help if you cut-n-paste the exact error messages, when you ran the commands that you "already tried".

Henry




The area marked by white rectangle is where I generated the two Baz classes in different locations and what I tried to compile FindBaz.java the way what you said to me.

I still can't compile Henry Wong.help me out!Thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Ian Su wrote:
I still can't compile Henry Wong.help me out!Thanks!


Oops... I can't believe that I forgot about this.

Short answer. You can't do it. You are not allowed to use a class that is in the default package from any named package.

Long answer. If you are trying to use a class that is in a different package, and not in the java.lang package, then you must import the class. In this example, the FindBaz class must import the Baz class because the Baz class is in a different package, and not the java.lang package. In older versions of Java, I believe you can just do an import with the name of the class. This is no longer allowed. If you want the FindBaz class to use the Baz class, then the Baz class needs to also be in a package.

Henry
Ian Su
Greenhorn

Joined: Nov 18, 2010
Posts: 25
Henry Wong wrote:
Ian Su wrote:
I still can't compile Henry Wong.help me out!Thanks!


Oops... I can't believe that I forgot about this.

Short answer. You can't do it. You are not allowed to use a class that is in the default package from any named package.

Long answer. If you are trying to use a class that is in a different package, and not in the java.lang package, then you must import the class. In this example, the FindBaz class must import the Baz class because the Baz class is in a different package, and not the java.lang package. In older versions of Java, I believe you can just do an import with the name of the class. This is no longer allowed. If you want the FindBaz class to use the Baz class, then the Baz class needs to also be in a package.

Henry


Thank you,Henry!I really appreciate your help.
 
GeeCON Prague 2014
 
subject: Compiling problem for Scjp