This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiling a non packaged class

 
sumant sankaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sumant sankaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic