Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Easy Learn Java: Programming Articles, Examples and Tips - Page 149


Previous 1060 Stories (530 Pages, 2 Per Page) Next

Java Newsletters: Java Newsletters Archive: 20

Go to all tips in Java Newsletters

***********************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 20 17 January 2001 *
* http://www.javafaq.nu/java *
* *
* *
* Please recommend our FREE "100 Java Tips" book and us to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
***********************************************

Table of Contents

1. Are there any tools out there that will convert a program
writen in C to JAVA?
2. Can we create DLLs in java??? if yes How???
3. Is there a way to know from class X which class called the
method foo()?
4. Can someone tell me the difference between the JRE that comes
with the J2SDK and the stand-alone JRE?
5. Why I see applet in applet viewer and do not in a browser?
6. Whenever I compile my servlets it always says "can't find
package javax.*" even though I downloaded the JSDK...
7. Why does this simple application never exit?
***********************************************

Hello dear friends!

***********************************************

Tip 1 Q: Are there any tools out there that will convert a program
writen in C to JAVA?

Answer: Yes.

C2J: http://www.novosoft-us.com/NS2B.nsf/w1/C2J 
C2J has successfully compiled itself as well as programs such as
PGP and YACC. Obviously YMMV.

Based on C2J is a C++ to Java tool:
http://sol.pace.edu/~tilevich/c2j.html

***********************************************

Tip 2 Answer: Unfortunately it is impossible. DLL stands for
Dynamic Linking Library and has definite structure inside. DLL is
a part of executable code and helps to make an application for
Windows to be more smaller. And more flexible. It is something
like classes but compiled (Java class files are byte codes and
JDK compiles them during the runtime...).
In Java it is not possible to make an executable code.
But with third party software Yes! 
See native compilers on our site. 
But I didn't hear about creating DLLs.

So my answer is: in Java it is not possible, but with third party
applications it is possible theoretically. Although I do not know
any compilers that produce DLLs
there is no limitations to do that....

John

***********************************************

Tip 3 Q: Is there a way to know from class X which class called the
method foo()?

Answer: If class A and class B are calling a method foo() on class X,
is there a way to know from class X which class called the method
foo() (they can be either A or B). I know that this can be done by
capturing the stack trace and examining it, but that solution looks
expensive as I have to create a new Throwable object every time and
capture stack trace (And I do this quite frequently).

Is there any other elegant solution to do this, any help and direction
is appreciated.

Answer: Pass a reference to the class to the foo() method.

foo(Object x){
System.out.println(x.getClass());
}

should do it.

***********************************************

Tip 4 Q: Can someone tell me the difference between the JRE that
comes with the J2SDK and the stand-alone JRE?
Can someone tell me the difference between the JRE that comes
with the J2SDK and the stand-alone JRE? When should I use which?
I read the sun's web page and they said the the JRE stand-alone
package is for shipping your application, it doesn't come with a
compiler nor debugger, but what does it really mean?

Answer: Exactly that. The Java 2 SDK (aka JDK) is the JRE plus
the compiler (javac) and debugger (jdb). The JRE is entirely
sufficient to run a Java application (with a couple exceptions
in situations where you call into the compiler or some such).
It's entirely redistributable with your Java application if
you've written an app in Java intended for an audience that
may not have the JRE installed.

***********************************************

Tip 5 Q:Why I see applet in applet viewer and do not in a browser?
When i try to view my applet on a web page i get the error

java.lang.NoSuchMethodError: java/lang/Double: method
parseDouble(Ljava/lang/String;)D not found

Which is weird as it compiles fine on Borland and with the JDK
using applet viewer
Anyone have any ideas what is going wrong?

Answer: The parseDouble method was only added to Java in JDK 1.2
Browsers typically only support Java 1.1 
If you have the JRE installed, you can run Java 1.2 applets. But
you must also change the HTML code that embeds the applet. Check
javasoft.com. I believe they have a program which will
automatically change the "APPLET" tag to "EMBED" and add whatever
else is needed. It's been a while since I've done applets but I
do remember running across a similar problem.

***********************************************

Tip 6 Q: Whenever I compile my servlets it always says "can't
find package javax.*" even though I downloaded the JSDK. Where
the JSDK files so it'll find that package?

Answer: There are no classes in the javax.* package. There are
classes in javax.servlet.* and javax.servlet.http.*, but neither
are really related to javax.* -- importing javax.* won't affect
them. You should import the packages that you really want to use!

***********************************************

Tip 7 Q: Why does this simple application never exit?

public class UIQuitTest {
public static void main (String[] args) {
java.awt.Frame f = new java.awt.Frame();
f.dispose();
f = null;
} // end of main ()
}

The application above never quits, is it a bug or a (mis)feature?
Win98, JRE 1.3.0

Answer: By creating an AWT object, you now have started the AWT
thread. In order to end the application now, you have to do
a System.exit(0) that will kill all non-daemon threads,
including the AWT thread.

The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

Copyright (c) 2000 John Andersson



10 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Monday, February 02, 2004 (15:23:50) (2751 reads)

Java Newsletters: Java Newsletters Archive: 19

Go to all tips in Java Newsletters

***********************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 19 10 January 2001 *
* http://www.javafaq.nu/java *
* *
* *
* Please recommend our FREE "100 Java Tips" book and us to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
***********************************************

Table of Contents

1. I propose that Java should allow multiple inheritance if...
2. I got a problem with an array/vector...
3. Does anyone know how to make a file NOT "read-only"???
It is not simple question...
4. I have a question about sending a reference to the object
via the socket...
5. Why my program does not give the address of the local
machine on one PC and give on another?
6. Can applet corrupt my registry file?
7. I'm converting an old java client/server program which
is based on raw byte stream...
***********************************************

Hello dear friends!

***********************************************

Tip 1 Q: I propose that Java should allow multiple inheritance if...
Everyone knows the potential problem with multiple inheritance is
when you run into the problem of having two instances of a grand
parent super class.

For example:
class A extends D {int i; }
class B extends D {int i; }
class C extends A,B {}

Potentially, you could have two copies of D for each instance of C.

However, I propose that Java should allow multiple inheritance if
there are no instance variables associated with the abstracts that
the base class is extending.

abstract class A {
public setX();
public setY();
public setAll() {setX (); setY();
}
abstract class B {
public setC();
public setD();
public setBoth(){setC(); setD(); }

class C extends A,B {}

You won't have two instances of some grandfather class, since A and
B doesn't have instances variables.

I hope the next versions of Java explores this issue.

Answer: It does. They're called interfaces:

interface A {
public void setX();
public void setY();
public void setAll();
}
interface B {
public void setC();
public void setD();
public void setBoth();
}

interface C extends A,B {};

public abstract class D implements C {
}

jim

***********************************************

Tip 2 Q: I got a problem with an array/vector...
I got a problem with an array/vector.

my class contains a member:
static Vector quad[][];
....

in my constructor I got:
Vector quad[][] = new Vector[row][col];
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
quad[i][j] = new Vector (0,1);
}
}

// row and col are int between (10..50) -- it's a big I know, but
that might not be the problem

My PROBLEM (and I don't know what to do, really), I can't access
quad[x][y] outside of the constructor!!!! Within the constructor
I've got full access on quad[x][x]. Java (1.2) returns a
NullPointerException on any method except within the constructor!!!

What's my fault!???

Answer: I guess you shouldn't write Vector here:
Vector quad[][] = new Vector[row][col];
so, the correct variant may be:
quad[][] = new Vector[row][col];

I guess You just overridden your static variable with one defined
in your constructor:
Vector quad[][].
Thus, you're initializing NOT your class-scope static variable
but your constructor-scope quad. It's not reachable outside the
constructor. And as for static quad, it has never been initialized!
And a first reference to it causes NullPointerException. I guess.
I hope I'm right Smile

***********************************************

Tip 3 Q: Does anyone know how to make a file NOT "read-only"???
Does anyone know how to make a file NOT "read-only"?? I know how
to make a File read-only, but I do not know how to do the opposite.
I have consulted the JavaDoc for File, FileDescriptor, FileSystem
and have not found a way of doing this. Any help would be greatly
appreciated.

To set Read-Only:
File file = new File( "c:/testFile.txt" );
file.setReadOnly();

A: You can't from Java. See:
http://developer.java.sun.com/developer/bugParade/bugs/4167472.html

I find Sun's argument rather weak however.

You can get around the problem by calling Runtime.exec() with a
command like "chmod" specific to your operating system, in order to
modify the file mode.

***********************************************

Tip 4 Q: I have a question about sending a reference to the object
via the socket...
I have a question about sending a reference to the object via the
socket. Two threads are communicating via sockets running on the
same machine. I don't need to send the whole object, but I need
to send just a reference.
Does anyone knows how to do that?

Answer: Reference to an Object? A reference is only valid within
the same memory space! If you want to be able to invoke methods
on an object remotely, then you will need to use a remote technology
like RMI, CORBA, or some such.

***********************************************

Tip 5 Q: Why my program does not give the address of the local
machine on one PC and give on another?

Answer: As long as you have TCP/IP installed, you should at
least get 127.0.0.1


***********************************************

Tip 7 Q: I'm converting an old java client/server program which
is based on raw byte stream heavily into new one which requires
utilizing object streams. But if I open input/output object
streams on both side this blocks system and won't proceed.


ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new
ObjectOutputStream(socket.getOutputStream());

Answer: Upon opening the ObjectInputStream, the constructor blocks
to read a header from the stream (doing who-knows-what). Now, what
happens is, both your client and server open the InputStream... and
happily wait forever for the other side to send them the header
they want. Deadlock guaranteed!
The solution is simple: open the ObjectOutputStream first on at
least one side, but better on both sides (usually, symmetry == good).
Problem solved
Smile
You are trying to keep two streams going at once independently, right.
That means you need at least two threads at each end.

The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

Copyright (c) 2000 John Andersson
***********************************************
* *
* You can find our tips on site also! *
* Please recommend us and our FREE "100 Java Tips" book to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
* *
***********************************************



6 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Monday, February 02, 2004 (15:19:42) (2341 reads)

Previous 1060 Stories (530 Pages, 2 Per Page) Next

530| 529| 528| 527| 526| 525| 524| 523| 522| 521| 520| 519| 518| 517| 516| 515| 514| 513| 512| 511| 510| 509| 508| 507| 506| 505| 504| 503| 502| 501| 500| 499| 498| 497| 496| 495| 494| 493| 492| 491| 490| 489| 488| 487| 486| 485| 484| 483| 482| 481| 480| 479| 478| 477| 476| 475| 474| 473| 472| 471| 470| 469| 468| 467| 466| 465| 464| 463| 462| 461| 460| 459| 458| 457| 456| 455| 454| 453| 452| 451| 450| 449| 448| 447| 446| 445| 444| 443| 442| 441| 440| 439| 438| 437| 436| 435| 434| 433| 432| 431| 430| 429| 428| 427| 426| 425| 424| 423| 422| 421| 420| 419| 418| 417| 416| 415| 414| 413| 412| 411| 410| 409| 408| 407| 406| 405| 404| 403| 402| 401| 400| 399| 398| 397| 396| 395| 394| 393| 392| 391| 390| 389| 388| 387| 386| 385| 384| 383| 382| 381| 380| 379| 378| 377| 376| 375| 374| 373| 372| 371| 370| 369| 368| 367| 366| 365| 364| 363| 362| 361| 360| 359| 358| 357| 356| 355| 354| 353| 352| 351| 350| 349| 348| 347| 346| 345| 344| 343| 342| 341| 340| 339| 338| 337| 336| 335| 334| 333| 332| 331| 330| 329| 328| 327| 326| 325| 324| 323| 322| 321| 320| 319| 318| 317| 316| 315| 314| 313| 312| 311| 310| 309| 308| 307| 306| 305| 304| 303| 302| 301| 300| 299| 298| 297| 296| 295| 294| 293| 292| 291| 290| 289| 288| 287| 286| 285| 284| 283| 282| 281| 280| 279| 278| 277| 276| 275| 274| 273| 272| 271| 270| 269| 268| 267| 266| 265| 264| 263| 262| 261| 260| 259| 258| 257| 256| 255| 254| 253| 252| 251| 250| 249| 248| 247| 246| 245| 244| 243| 242| 241| 240| 239| 238| 237| 236| 235| 234| 233| 232| 231| 230| 229| 228| 227| 226| 225| 224| 223| 222| 221| 220| 219| 218| 217| 216| 215| 214| 213| 212| 211| 210| 209| 208| 207| 206| 205| 204| 203| 202| 201| 200| 199| 198| 197| 196| 195| 194| 193| 192| 191| 190| 189| 188| 187| 186| 185| 184| 183| 182| 181| 180| 179| 178| 177| 176| 175| 174| 173| 172| 171| 170| 169| 168| 167| 166| 165| 164| 163| 162| 161| 160| 159| 158| 157| 156| 155| 154| 153| 152| 151| 150|
149
| 148| 147| 146| 145| 144| 143| 142| 141| 140| 139| 138| 137| 136| 135| 134| 133| 132| 131| 130| 129| 128| 127| 126| 125| 124| 123| 122| 121| 120| 119| 118| 117| 116| 115| 114| 113| 112| 111| 110| 109| 108| 107| 106| 105| 104| 103| 102| 101| 100| 99| 98| 97| 96| 95| 94| 93| 92| 91| 90| 89| 88| 87| 86| 85| 84| 83| 82| 81| 80| 79| 78| 77| 76| 75| 74| 73| 72| 71| 70| 69| 68| 67| 66| 65| 64| 63| 62| 61| 60| 59| 58| 57| 56| 55| 54| 53| 52| 51| 50| 49| 48| 47| 46| 45| 44| 43| 42| 41| 40| 39| 38| 37| 36| 35| 34| 33| 32| 31| 30| 29| 28| 27| 26| 25| 24| 23| 22| 21| 20| 19| 18| 17| 16| 15| 14| 13| 12| 11| 10| 9| 8| 7| 6| 5| 4| 3| 2| 1|


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy