aspose file tools*
The moose likes Beginning Java and the fly likes compiling a non packaged class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compiling a non packaged class" Watch "compiling a non packaged class" New topic
Author

compiling a non packaged class

sumant sankaran
Greenhorn

Joined: Sep 16, 2006
Posts: 4
Using JDK 1.4

Folks would be great if you could help me out here :


Lets say I have a file called Sample1.java under


<directory>/a/b :


The file Sample1.java is as follows :
package a.b;
public class Sample1
{
public Sample1()
{


}


public static void main(String []args)
{
Sample a =new Sample();
System.out.println("hello world");
}



}


and under <directory>
I have got a class.jar that contains just Sample.class ( no package or
anything,just the class file)

How do I compile this??
I do the following:
<directory>/a/b/javac -classpath .:<directory>/class.jar Sample1.java


I get the following error:
--------------------------------------------------------------
Sample1.java:12: cannot resolve symbol
symbol : class Sample
location: class a.b.Sample1
Sample a =new Sample();
^
Sample1.java:12: cannot resolve symbol
symbol : class Sample
location: class a.b.Sample1
Sample a =new Sample();
--------------------------------------------------------------


How do I resolve this issue?What am I doing wrong here.
Please dont ask me to put the Sample.class in a package , I wont be
able to do that since that jar is actually already available.


Thanks
Sumant
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,

Welcome to JavaRanch!

As of JDK 1.4, javac won't accept the "import ClassName" syntax anymore. It was historically unclear whether this was legal or not; in JDK 1.4, this was clarified: it's not legal code. There's no way to import an unpackaged class into a packaged class.

If you're not using any newer language features, maybe you could get hold of an older compiler and use that. Alternatively, you could use reflection to create and manipulate the object.


[Jess in Action][AskingGoodQuestions]
sumant sankaran
Greenhorn

Joined: Sep 16, 2006
Posts: 4
Thanks Ernest.Unfortunately I might not be able to use the older compiler,we are already using jdk14 for the project.
How can I go about doing this using reflection any pointers on that?If reflection works then I could probably try to use that everywhere where I face this issue ?
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226

How do I resolve this issue?What am I doing wrong here.
Please dont ask me to put the Sample.class in a package , I wont be
able to do that since that jar is actually already available.


Getting rid of the correct answer is a bad way to start. Since these are called Sample and Sample one I'd make a guess that you have the source to Sample. Go back and fix it.
sumant sankaran
Greenhorn

Joined: Sep 16, 2006
Posts: 4
Tim,
This was just an example to illustrate a similar problem I was facing. I dont have the source code of the file akin to Sample in the actual scenario.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Using reflection means doing something like this (from memory, sorry if I get the parameter lists a little bit wrong). In this code, I load a class named "Sample", create an instance of it using a no-argument constructor, and call a no-argument method "aMethodName" on it, putting the return value in variable "result."

import java.lang.reflect.*;
// ...
Class clazz = Class.forName("Sample");
Object sample = clazz.newInstance();
Method m = clazz.getMethod("aMethodName", new Class[0]);
Object result = m.invoke(sample, new Object[0]);
sumant sankaran
Greenhorn

Joined: Sep 16, 2006
Posts: 4
Thanks Ernest,Using reflection helped me out.I need to make these changes in a lot of files now .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by sumant sankaran:
Thanks Ernest,Using reflection helped me out.I need to make these changes in a lot of files now .


Perhaps you could write a packaged class named "Sample" with the same methods as your unpackaged one, and implement all the methods by forwarding them to the unpackaged class using reflection. Then perhaps all you'd need to fiddle with would be import statements in all those files.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: compiling a non packaged class
 
Similar Threads
error when testing...any help will be appreciated
Compile Question/Error
How can I get my HelloWorld Servlet to Run?
Unable to found class
Habibi book--HELP-I have a problem compiling the source as outlined on pages 319-320