• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

EXERCISE 1-1, Java OCA, Creating an Abstract Superclass and Concrete Subclass

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I create the following directory structure:

E:\demo\food\Fruit.java
E:\demo\test\Apple.java

Here is the Fruit class code:

package demo.food;
public abstract class Fruit {
public abstract String Hello(String a);
}

This Fruit code works

Here is the Apple class code:

package demo.test;
import demo.food.Fruit;
class Apple extends Fruit {

public String Hello(String a) {
return "Hello, im Apple extends Fruit !";
}

public static void main(String[] args) {
Apple apple = new Apple();
System.out.print(apple.Hello("Thanh"));
}

}

This code doesn't compile

Apple.java:2: error: package demo.food does not exist
import demo.food.Fruit;
               ^
Apple.java:3: error: cannot find symbol
class Apple extends Fruit {
                   ^
 symbol: class Fruit
2 errors

I need help, thanks !









 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cherry:

First two things, by intention we ask everyone to provide their Real Names here for use in the forum.
We aren't generally discussing how to obtain Banned Substances on the Dark Web or how to hide a body after a murder, so that shouldn't usually be a problem.

The rationale behind this is discussed here:
https://coderanch.com/wiki/718810/CodeRanch-naming-policy

Also, tho in this case the code is so simple it didn't make much difference to me personally, you are going to want to get used to using "Code Tags" correctly:
https://coderanch.com/wiki/659781/Code-Tags

When you do that, you will find way more people read your code and are likely to have helpful responses to contribute.

Anyway, the reason you came here, "What's going on with compiling at the command line??"

This is one of the more annoying parts of studying for the OCA or OCJP exams.

In "Real Life", you don't do all that much compiling from the command lines, but it is an important part of basic Java knowledge and is probably still covered on the exam.
At least on the OCJP, command line stuff without modules isn't any more, I think, but whatever.  Check your reference sources for whichever exam you are preparing for, and this is pretty basic stuff anyway.

So let's get this right for now and most of the rest of everything you are studying will be more Practical, more Theoretical, and more Fun.

There are two ways of compiling stuff (without modules) from the command line.

One is using the "-d <directory>" option which I daresay is the more common.
This will place all your .class files under the <directory> that you specify, respecting the package names.

You are not using this form right now, so both the compile and run steps are very sensitive to where you are sitting in your directory structure when you issue them.

If you don't use the "-d <directory>" option, then each .java file you compile will generate a .class file in that exact same directory the .java file sits in.

So given your example, the following will work (I know because I tried it) from my PowerShell prompt, creating directory Demo on my F: drive where I keep all code:
PS F:\> javac demo/food/Fruit.java
PS F:\> javac demo/test/Apple.java
PS F:\> java demo/test/Apple
Hello, im Apple extends Fruit !


Note that when you are doing things this way you specify the full path in terms of package name to both your .java files and your .class file

There are other things to know about compiling from the command line, but let's see if doing just this works for you so far.
 
duc thanh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, my problem is solved, can you tell me more about the -d option?
 
Jesse Silverman
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last answer here might clear things up (the names of the files and classes are all different of course)...
https://coderanch.com/t/736630/java/cp-javac-command
I'd possibly ignore the first few replies on the thread, the OP was confused...
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic