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

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 ]
 
machines help you to do more, but experience less. Experience this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic