• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Package Problem - Help Please

 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,
I am facing a new problem in java.

I have written a class without any package structure lets call it as Test.java.

and i have one package called com\i2. I am writing a new class in that called
Hello.java

Sample Code is below
<<<<<<TEST.JAVA>>>>>>> This is in default Package

public class Test
{
public void add()
{
System.out.println("Addd!");
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}



<<<<<,HELLO.JAVA>>>.. this is in com\i2 package
package com.i2;

public class Hello
{public static void main(String[] args)
{
Test a = new Test();
a.add();
System.out.println("Hello World!");}}


i am getting error in the 3 line while compiling com\i2\Hello.java

I tried like this
javac -classpath D:\exam exam\com\i2\*.java

exam\com\i2\Hello.java:5: cannot resolve symbol
symbol : class Test
location: class com.i2.Hello
Test a = new Test();

I am keeping the Test.class in d:\exam and all my classpath are pointed to d:\exam

cant we refer the default package class from within a class under a package structure..
PLEASE HELP ME


i tried like this too
In Hello.java , i used import Test;

i get '.' expected import Test; error....

PLEASE TELL me a solution to the above
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't do it. The compiler won't let you import classes from the default package into a file in a named package. This is done deliberately, to discourage the use of the default package.

If you desperately need to do this because Test is a class from a 3rd party library and you don't have the source, you could do it using reflection -- i.e., load the Class object using Class.forName(), create an instance with Class.newInstance() or Constructor.newInstance(), and call methods reflectively.
 
Karthik Rajendiran
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THank you very much for ur kind reply
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way is to write a wrapping class in a named package, and compile it with JDK 1.3, because this compilation rule was introduced in JDK 1.4.

--Herong
[ November 12, 2005: Message edited by: Herong Yang ]
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic