0

I am extremely new in Java and I couldn't get one thing straight. I have E:\Java\ACP\Cricket\ directory where I have a Main.java, Player.java and CricPlayer.java. Now I am inheriting Player class into CricPlayer. I have Main.Java as

import Java.ACP.Cricket.*;
public class Main
{
    public static void main(String[] args)
    {
        System.out.println("Hello.");
    }
}

CricPlayer.java As...

import Java.ACP.Cricket
public class CricPlayer extends Player
{
}

I need to use objects of CricPlayer in Main.java. As I mentioned earlier I am real new to Java so there might be some really obvious mistakes that I don't know about. But the question is how can I create a package for use in CricPlayer.java and Main.java? Because each time I compile the code using CMD it says "CricPlayer.java:6: error';' expected import java.ACP.Cricket.*" and points ^ to *. I don't know if I haven't created the Package properly or if it's some syntactical issue.

Edd
  • 3,724
  • 3
  • 26
  • 33
Abbas
  • 3,529
  • 5
  • 36
  • 64
  • Are the jar files located in E:\Java\ACP\Cricket folder because if the jars are not located their you need to link them in the compiler. http://stackoverflow.com/questions/6069702/java-command-line-with-external-jar – TheBetaProgrammer Apr 03 '14 at 13:06

6 Answers6

0

in the class below you shourd write a package (folder in project where file is situated) for example package ACP.Cricket;

Mikhail
  • 2,690
  • 4
  • 28
  • 43
0

You forgot the ; at your import in CricPlayer class:

import Java.ACP.Cricket.*;

Now you should be able to create CricPlayer Objects in your Main Class.

Lukas Warsitz
  • 1,231
  • 1
  • 11
  • 20
0

Also I don't know if "Java" in your package name is supposed to be capitalized or not. Seems like the compiler is expecting a lower case 'J' from your naming convention

the_constant
  • 681
  • 4
  • 11
0

you need to add package declaration in the beginning of your code files. And you don't need imports if all the classes reside in the same package. BTW, good coding convention in java recommends that all letters in package name are lowercase :)

package Java.ACP.Cricket;
public class Main
{
// Your class contents
}
  • Ok now I got that error removed but the compiler is now saying that it can not find symbol "Player" in CricPlayer.java. – Abbas Apr 03 '14 at 13:12
  • Also when I compile javac Player.java it doesn't create Player.class in the directory although there are no errors during the compilation. @Kristjan Veskimäe – Abbas Apr 03 '14 at 13:17
  • Go in your command prompt into root catalog E:\ and compile 'javac Java/ACP/Cricket/Player.java' and should be fine. BTW you only need to compile the class that uses other classes, those dependant classes will be compiled automagically. – Kristjan Veskimäe Apr 03 '14 at 13:24
  • I tried that but the compiler still doesn't know about the CricPlayer or Player Identifier in the code I have written in the code package Java.ACP.Cricket.CricPlayer; public class Main { public static void main(String[] args) { System.out.println("Hello"); CricPlayer obj; } } and the compiler says the package Java.ACP.Cricket doesn't exist! Also it declare CricPlayer to be undeclared, I believe it to be because of the package. – Abbas Apr 03 '14 at 13:49
  • A little new to stackoverflow forgot to tag you. :) – Abbas Apr 03 '14 at 13:57
0

Vincenzzochi is right, packages are lower case (but it should not break compilation). And you need to declare the package at the begining of all your classes : package java.acp.cricket;

And while all your source files are in the same package, you don't have to import anything!

Romain
  • 301
  • 2
  • 7
0

Here's an alternative to your Programs.I've verified this myself, so you can be assured that it will work.

Important thing you need to know, is that any file can be compiled only if you have a main method for that file(ie in that class). so here's a detailed 'How to do it':

PlayerDemo.java:

package Java.ACP.Cricket;
class Player{
//class members & methods
}
class PlayerDemo{
    public static void main(String[] args){
        System.out.println("Player Class");
    }
}   

CricPlayerDemo.java:

package Java.ACP.Cricket;
class CricPlayer extends Player{
// class members & methods
}
class CricPlayerDemo{
    public static void main(String[] args){
    System.out.println("CricPlayer Class");
    }
}   

Main.java:

package Java.ACP.Cricket;
class Main{
    public static void main(String[] args){
    System.out.println("Main Class");
//use other class objects here directly.
    }
}   

In E: , do following stepwise:

1> javac PlayerDemo.java (this creates Player.class & PlayerDemo.class)
2> copy Player.class to Cricket folder.
3> javac CricPlayerDemo.java (this creates CricPlayer.class & CricPlayerDemo.class)
4> copy CricPlayer.class to Cricket folder.
5> javac Main.java 
6> copy Main.class to Cricket folder.
7> now,also in E:, java Java/ACP/Cricket/Main 

Instead of compiling from E:, you can also compile from Cricket,to avoid copying class files,but it wont work for CricPlayerDemo.java, as CricPlayer extends Player(whose location is acc to package) so you'll have to compile CricPlayerDemo from E:

Sumedh
  • 404
  • 3
  • 11