Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

URLClassLoader problem

1 view
Skip to first unread message

Juergen Berger

unread,
May 17, 2000, 3:00:00 AM5/17/00
to
Hello,

If I start the small Test program below I will get an error and
I do not know why and how to fix it.

O:\DATA\java\URLtest>java Test PrintOut
java.lang.InstantiationException: PrintOut
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Unknown Source)
at Test.main(Test.java:15)


/*--------------------------------------------------------------------------
! Test
--------------------------------------------------------------------------*/
import java.net.*;

public class Test
{

/*--------------------------------------------------------------------------
! main

--------------------------------------------------------------------------*/
public static void main ( String args[]) throws Exception
{
try
{
URL[] urls = new URL[]{new URL ("file:V/fdk/test/")};
ClassLoader cl = new URLClassLoader(urls);
Class c = cl.loadClass(args[0]);
c.newInstance(); <== error occurs here
}
catch ( MalformedURLException e) {e.printStackTrace();}
catch ( ClassNotFoundException e){e.printStackTrace();}
catch ( IllegalAccessException e){e.printStackTrace();}
catch ( InstantiationException e){e.printStackTrace();}
PrintOut po = new PrintOut("jj");
}

}

/*--------------------------------------------------------------------------
! PrintOut
--------------------------------------------------------------------------*/
public class PrintOut
{
private String str;

public PrintOut (String Str)
{ str = Str;
}
public void print ()
{
System.out.println(str);
}
public void print ( String Str )
{
str = Str;
print();
}
}


Thank you
J.Berger

Juergen.Berger.vcf

klaus.h...@web.de

unread,
May 17, 2000, 3:00:00 AM5/17/00
to
In article <3922440C...@alcatel.de>,
Juergen Berger <Juergen...@alcatel.de> wrote:
> This is a multi-part message in MIME format.
> --------------192B1F68D414A3CD103A1150
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit

>
> Hello,
>
> If I start the small Test program below I will get an error and
> I do not know why and how to fix it.
>
> O:\DATA\java\URLtest>java Test PrintOut
> java.lang.InstantiationException: PrintOut
> at java.lang.Class.newInstance0(Native Method)
> at java.lang.Class.newInstance(Unknown Source)
> at Test.main(Test.java:15)
>
...

> Class c = cl.loadClass(args[0]);
> c.newInstance(); <== error occurs here
> }
...

> public class PrintOut
> {
> private String str;
>
> public PrintOut (String Str)
> { str = Str;
...


Versuch doch mal einen Default-Constructor einzufuehren:

public PrintOut( ) {
}

Vielleicht hilft's.
Klaus.


Sent via Deja.com http://www.deja.com/
Before you buy.

Marius Strumyla

unread,
May 17, 2000, 3:00:00 AM5/17/00
to
this is because your PrintOut class doesn't has a default constructor.
public PrintOut()
{}
Class.newInstance() tries to call the empty constructor and fails

Juergen Berger wrote:
>
> Hello,
>
> If I start the small Test program below I will get an error and
> I do not know why and how to fix it.
>
> O:\DATA\java\URLtest>java Test PrintOut
> java.lang.InstantiationException: PrintOut
> at java.lang.Class.newInstance0(Native Method)
> at java.lang.Class.newInstance(Unknown Source)
> at Test.main(Test.java:15)
>

> /*--------------------------------------------------------------------------
> ! Test
> --------------------------------------------------------------------------*/
> import java.net.*;
>
> public class Test
> {
>
> /*--------------------------------------------------------------------------
> ! main
>
> --------------------------------------------------------------------------*/
> public static void main ( String args[]) throws Exception
> {
> try
> {
> URL[] urls = new URL[]{new URL ("file:V/fdk/test/")};
> ClassLoader cl = new URLClassLoader(urls);

> Class c = cl.loadClass(args[0]);
> c.newInstance(); <== error occurs here
> }

> catch ( MalformedURLException e) {e.printStackTrace();}
> catch ( ClassNotFoundException e){e.printStackTrace();}
> catch ( IllegalAccessException e){e.printStackTrace();}
> catch ( InstantiationException e){e.printStackTrace();}
> PrintOut po = new PrintOut("jj");
> }
>
> }
>
> /*--------------------------------------------------------------------------
> ! PrintOut
> --------------------------------------------------------------------------*/

> public class PrintOut
> {
> private String str;
>
> public PrintOut (String Str)
> { str = Str;
> }

> public void print ()
> {
> System.out.println(str);
> }
> public void print ( String Str )
> {
> str = Str;
> print();
> }
> }
>
> Thank you
> J.Berger

--
Marius Strumyla
programmer
No Magic, Inc.
mailto:mar...@nomagiclt.com
http://www.nomagic.com

MagicDraw UML http://www.magicdraw.com

Juergen Berger

unread,
May 18, 2000, 3:00:00 AM5/18/00
to
Yes it was the defaultconstructor.

J.Berger

Juergen.Berger.vcf
0 new messages