Comment On Porting Java

Love it or hate it, one of the great things about Java is its portability. It abstracts out many of the platform-specific things so that you, the programmer, need not worry about things like file systems and how to create directories and the like. That said, Nick Smith was a bit surprised to learn that he was tasked with porting a Java application that ran on a UNIX host onto a Windows server. Not a big deal, he thought, that's what Java was built for ... [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: Porting Java

2005-12-20 14:30 • by Ekevu
Ah!, the wonderful benefits of knowing the system you're running on! :-)

Re: Porting Java

2005-12-20 14:34 • by whojoedaddy
Haha, that's hilarious. Doesn't even check the os to see if it's unix first.

Re: Porting Java

2005-12-20 14:36 • by Manni
53983 in reply to 53981

Whew it's a good thing that every operating system has a command-line interface that supports the internationally standardized "mkdir" command.


I hear that there are talks to standardize the command frequently used in movies known as "PASSWORD OVERRIDE". I use it all the time.

Re: Porting Java

2005-12-20 14:36 • by Jim
of course!!  its the way i ensure my code runs on the intended operating system i design it for.  who wants portability when theres functionality that works?  

Re: Porting Java

2005-12-20 14:37 • by DiamondDave
53985 in reply to 53981
whojoedaddy:
Haha, that's hilarious. Doesn't even check the os to see if it's unix first.




Well, if the original author of this code bothered to read the Java API, they'd realize how stupid this code really is.

Re: Porting Java

2005-12-20 14:37 • by aprenot

Perfect example of the ways that a programmer can totally break the framework and intentions of something like Java.  You would think that it would have been easier to use Java's IO classes to achieve this, but I guess not. 

Re: Porting Java

2005-12-20 14:42 • by coward
53987 in reply to 53986
might as well have writen a short shell script.

Re: Porting Java

2005-12-20 14:44 • by Anonymous Coward

Inability to write portable code seems quite common among unixoids. Typical WTFs include:


 



  • Using shell commands for everything, including operations that are provided by the language (e.g., mkdir).
  • Using regular expressions for all string comparisons, even trivial stuff like "is the first character in the string a Q".

Re: Porting Java

2005-12-20 14:45 • by osp70
If he had made it completely portable then there would be one less job created to make this usable on other systems.  He was just protecting the interests of all the coders that do upgrades for a living.

Re: Porting Java

2005-12-20 14:45 • by mlathe
53990 in reply to 53987

This kind of stuff happens a lot in PERL too.


but sometimes its just so easy to


cat tmpfile| sort

Re: Porting Java

2005-12-20 14:50 • by JavaBean
If that's the worst violation he has to deal with, this "port" will be trivial, and will run better on the original platform to boot!

Re: Porting Java

2005-12-20 14:50 • by BiggBru

Wow. The portability of Java, minus the stupidity of the programmer, equals useless code.


Then again, how can we expect him to know that Windows and Unix require different commands? Honest mistake, right?


[:P]

Re: Porting Java

2005-12-20 14:55 • by RevMike
Alex Papadimoulis:

...Nick Smith was a bit surprised to learn that he was tasked with porting a Java application that ran on a UNIX host onto a Windows server...



Well, that is the main WTF right there.  Why would you port something from Unix to Windows?  Unix to Linux makes sense.  If the server is connected to a public network, Unix to OpenBSD makes even more sense.  But why Unix to Windows?

At least just install cygwin and call it a day.

Re: Porting Java

2005-12-20 14:59 • by boohiss
Just because Java is extremely portable, doesn't mean the software being developed is going to be used on every platform.

Sure, it's a best practice to use more abstract functions, but this WILL work as long as the 'mkdir' command exists.

This is a very minor WTF.

Re: Porting Java

2005-12-20 15:06 • by Volmarias
53996 in reply to 53995
I'd say it's a minor wtf, but not because the person "knew" ahead of time that it was only going to be used on one platform. I mean, creating the temp directory is kind of stupid, but I can see a valid reason that you'd want to do it.

Maybe he was just concerned that it would take too long for java create a directory, thanks to the performance boogeyman, or it would take him too long to look at the docs and figure out how to do that. I'd consider this a hack, and not a particularly ugly one (homely, perhaps). This reeks of the bottom of the barrel of wtfs; is the end near?

Re: Porting Java

2005-12-20 15:06 • by ammoQ
That's the quick-and-dirty solution for people who don't like to RTFM.
I admit that todays WTF could almost be mine. (Of course it isn't, I
would not have a "Constants" class, but rather write

Runtime.getRuntime().exec("mkdir -p /var/temp/foo/bar/uploads") or omit it at all, since the directory _must_ exist.



Sidenote: mkdir -p creates all the necessary directories on the way.

Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.

Re: Porting Java

2005-12-20 15:06 • by Volmarias
53998 in reply to 53996
I just reread my post. Need more coffee for lucidity :(

Re: Porting Java

2005-12-20 15:13 • by TankerJoe
Alex Papadimoulis:

public static MultipartRequest createMultipartRequest (HttpServletRequest req) 
throws IOException
{
[SNIP]

// Create a temp directory for the attachments
Runtime.getRuntime().exec ("mkdir -p " + Constants.UPLOAD_PATH);

File check = new File (Constants.UPLOAD_PATH);

[SNIP]

}




I would say that his way was highly optimised, BUT he uses a constant to hold the UPLOAD_PATH instead of putting it in XML!

OTOH, look how convienient it is to get the static member out Constants
"class".  OOP at its best.  Anyways, for those who don't
know, the "correct" way to do this is:



public static MultipartRequest createMultipartRequest (HttpServletRequest req) 
throws IOException
{
[SNIP]

File check = new File (Constants.UPLOAD_PATH);
if (!check.exists(){
check.mkdirs(); //creates the directory and any parent directories needed.
}

[SNIP]

}









Re: Porting Java

2005-12-20 15:24 • by kipthegreat
54001 in reply to 53997
ammoQ:
That's the quick-and-dirty solution for people who don't like to RTFM.
I admit that todays WTF could almost be mine. (Of course it isn't, I
would not have a "Constants" class, but rather write

Runtime.getRuntime().exec("mkdir -p /var/temp/foo/bar/uploads") or omit it at all, since the directory _must_ exist.



Sidenote: mkdir -p creates all the necessary directories on the way.

Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.


Presumably, the original author wrote more than just these lines in Java.  So it's not like they were unfamiliar with the language and how to use the javadoc to find something you're looking for..  I mean, they had to lookup Runtime.getRuntime().exec(), File.mkdirs() doesn't seem much harder...

Re: Porting Java

2005-12-20 15:26 • by Mike
Maybe the original developer was told the app would only be run on Unix.

Re: Porting Java

2005-12-20 15:28 • by TheDauthi
54003 in reply to 53994
RevMike:
Alex Papadimoulis:

...Nick Smith was a bit surprised to learn that he was tasked with porting a Java application that ran on a UNIX host onto a Windows server...



Well, that is the main WTF right there.  Why would you port something from Unix to Windows?  Unix to Linux makes sense.  If the server is connected to a public network, Unix to OpenBSD makes even more sense.  But why Unix to Windows?

At least just install cygwin and call it a day.



Even though I spend much of my time developing on unix servers, I can think of plenty of scenerioes in which it is reasonable to port to Windows:
Third-party software that relies on COM for interop.
Hardware drivers that are only available under Windows.
Customer requests version to work on their servers.
Support team prefers Windows.

And, of course, the most likely reason: the boss said so, because Windows' TCO is x% lower, and...

Since we're on the subject, I'll tell a similar, awful story on myself.  Back when I was a wee developer just leaving windows for the first time, I was writing some program or another and needed the current system's ip address.

Difficulty: I didn't know about the gethostname, gethostbyname, etc, functions.  Nor did I even know about using ioctl to grab the data.  I came up with something like
ifconfig |grep -A2 eth0 |grep inet|cut -d":" -f2 |cut -d" " -f1

Erm.  Yeah.  I know better now.

Re: Porting Java

2005-12-20 15:29 • by Pedant
54004 in reply to 54002
I've done something like this before.....



I needed to do a sort unique on a hundred odd meg file.

Re: Porting Java

2005-12-20 15:30 • by Rank Amateur

But this is portable code. Just ship it with the UNIX source included. Isn't that why they wrote it in C?


--Rank

Re: Porting Java

2005-12-20 15:33 • by TankerJoe
54006 in reply to 53997
ammoQ:
That's the quick-and-dirty solution for people who don't like to RTFM.
I admit that todays WTF could almost be mine. (Of course it isn't, I
would not have a "Constants" class, but rather write

Runtime.getRuntime().exec("mkdir -p /var/temp/foo/bar/uploads") or omit it at all, since the directory _must_ exist.



Sidenote: mkdir -p creates all the necessary directories on the way.

Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.




The problem is that interacting with the file system is such a basic
and intergral part of any programming language.  If he had to RTFM
to know that the java.io.File class could be used to create a directory
then he should not have been using Java to code this.  Quick,
think of your "favorite" language that you consider yourself skilled
in.  Can't you just say off the top of your head how to create a
directory or file?

Re: Porting Java

2005-12-20 15:36 • by RevMike
54007 in reply to 54003
TheDauthi:

Since we're on the subject, I'll tell a similar, awful story on myself.  Back when I was a wee developer just leaving windows for the first time, I was writing some program or another and needed the current system's ip address.

Difficulty: I didn't know about the gethostname, gethostbyname, etc, functions.  Nor did I even know about using ioctl to grab the data.  I came up with something like
ifconfig |grep -A2 eth0 |grep inet|cut -d":" -f2 |cut -d" " -f1

Erm.  Yeah.  I know better now.


Another milestone in the gowth from tadpole to frog is the first time you work on a system with more than one network adapter (excluding the loopback device).  Two IP addresses?  Two "host" names?  Boggle!

Re: Porting Java

2005-12-20 15:38 • by Rodpheus
54008 in reply to 53994
RevMike:
Alex Papadimoulis:

...Nick Smith was a bit surprised to learn that he was tasked with porting a Java application that ran on a UNIX host onto a Windows server...




Well, that is the main WTF right there.  Why would you port something from Unix to Windows?  Unix to Linux makes sense.  If the server is connected to a public network, Unix to OpenBSD makes even more sense.  But why Unix to Windows?

At least just install cygwin and call it a day.


Get out of academic life and come to real life

Re: Porting Java

2005-12-20 15:38 • by RevMike
54009 in reply to 54004
Anonymous:
I've done something like this before.....



I needed to do a sort unique on a hundred odd meg file.


Isn't that what Perl hashes are for?

I've actually done this for some "quick & dirty" tasks, and it worked fine.

Re: Porting Java

2005-12-20 15:40 • by RevMike
54010 in reply to 54008
Anonymous:
RevMike:
Alex Papadimoulis:

...Nick Smith was a bit surprised to learn that he was tasked with porting a Java application that ran on a UNIX host onto a Windows server...




Well, that is the main WTF right there.  Why would you port something from Unix to Windows?  Unix to Linux makes sense.  If the server is connected to a public network, Unix to OpenBSD makes even more sense.  But why Unix to Windows?

At least just install cygwin and call it a day.


Get out of academic life and come to real life



Ha!  Me? Academic?  You must be kidding!

Re: Porting Java

2005-12-20 15:40 • by ammoQ
54011 in reply to 54001
kipthegreat:

Presumably, the original author wrote
more than just these lines in Java.  So it's not like they were
unfamiliar with the language and how to use the javadoc to find
something you're looking for..  I mean, they had to lookup
Runtime.getRuntime().exec(), File.mkdirs() doesn't seem much harder...




But Runtime.getRuntime().exec()
is easier to find (really!) and it's also likely that it has been
abused before for similar operations, like moving files, for the folks
who haven't found File.renameTo().

Re: Porting Java

2005-12-20 15:42 • by ammoQ
54012 in reply to 54006
TankerJoe:
ammoQ:
That's the quick-and-dirty solution for people who don't like to RTFM.
I admit that todays WTF could almost be mine. (Of course it isn't, I
would not have a "Constants" class, but rather write

Runtime.getRuntime().exec("mkdir -p /var/temp/foo/bar/uploads") or omit it at all, since the directory _must_ exist.



Sidenote: mkdir -p creates all the necessary directories on the way.

Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.




The problem is that interacting with the file system is such a basic
and intergral part of any programming language.  If he had to RTFM
to know that the java.io.File class could be used to create a directory
then he should not have been using Java to code this.  Quick,
think of your "favorite" language that you consider yourself skilled
in.  Can't you just say off the top of your head how to create a
directory or file?




Create a file: yes. Create a directory: no.

Re: Porting Java

2005-12-20 15:44 • by RevMike
54013 in reply to 54011
ammoQ:
kipthegreat:

Presumably, the original author wrote
more than just these lines in Java.  So it's not like they were
unfamiliar with the language and how to use the javadoc to find
something you're looking for..  I mean, they had to lookup
Runtime.getRuntime().exec(), File.mkdirs() doesn't seem much harder...




But Runtime.getRuntime().exec()
is easier to find (really!) and it's also likely that it has been
abused before for similar operations, like moving files, for the folks
who haven't found File.renameTo().


Plus, the same technique can be used for things outside the common operations like creating a symlink in windows.

Re: Porting Java

2005-12-20 15:46 • by JohnO
54014 in reply to 53995

Anonymous:
Just because Java is extremely portable, doesn't mean the software being developed is going to be used on every platform. Sure, it's a best practice to use more abstract functions, but this WILL work as long as the 'mkdir' command exists. This is a very minor WTF.


Portability aside, you should always avoid shelling whenever possible because the error-handling is so poor.  I consider this a major WTF.  If you are going to do this, why not just call a shell script to do everything?  Do you open files with shell scripts?

Re: Porting Java

2005-12-20 15:49 • by JohnO
54015 in reply to 54004

Anonymous:
I've done something like this before.....

I needed to do a sort unique on a hundred odd meg file.


Nice...There's no sort function available in Java either.

Re: Porting Java

2005-12-20 15:49 • by Sanjeev
I am sure Nick Smith has his own reasons to hate Jave if his piece of art does not work :'(

Re: Porting Java

2005-12-20 15:49 • by cconroy
54017 in reply to 53997
ammoQ:
Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.





Agreed.  Gotta
love the fact that the "portable" solution involves locating a method
which is named after a particular OS's command for the operation. 
JavaDoc is only helpful if you already have a good idea of what you're
looking for.



Re: Porting Java

2005-12-20 15:51 • by JohnO
54018 in reply to 54011

ammoQ:
kipthegreat:

Presumably, the original author wrote more than just these lines in Java.  So it's not like they were unfamiliar with the language and how to use the javadoc to find something you're looking for..  I mean, they had to lookup Runtime.getRuntime().exec(), File.mkdirs() doesn't seem much harder...


But Runtime.getRuntime().exec() is easier to find (really!) and it's also likely that it has been abused before for similar operations, like moving files, for the folks who haven't found File.renameTo().


Maybe if you don't know alphabetical order and worked in shell scripts for 10 years before you began using Java?

Re: Porting Java

2005-12-20 15:53 • by RevMike
54019 in reply to 54015
JohnO:

Anonymous:
I've done something like this before.....

I needed to do a sort unique on a hundred odd meg file.


Nice...There's no sort function available in Java either.



java.util.SortedSet no good for you?

Re: Porting Java

2005-12-20 15:58 • by Joe Van Dyk
54020 in reply to 53990
In Ruby, it's not too much more difficult than the shell version:



File.readlines('tmpfile').sort



Probably the same exists in Perl.

Re: Porting Java

2005-12-20 15:59 • by Warp
54021 in reply to 53983
Manni:

I hear that there are talks to standardize the
command frequently used in movies known as "PASSWORD OVERRIDE". I use
it all the time.





Frequently? Can you mention more than one movie where you can see that command being written (or shown or whatever)?

Re: Porting Java

2005-12-20 16:00 • by Suck My Lisp
54022 in reply to 54019
RevMike:

java.util.SortedSet no good for you?


Well... SortedSet is an interface, so he'd probably want to use
TreeSet, unless he wanted to write his own data structure that
implemented SortedSet.



There's also Collections#sort and Arrays#sort functions that would be useful.



But I'd almost be willing to bet that, portability aside, spawning off
the Unix "sort" command to sort a large file would be a great deal
faster then doing it in Java.



Re: Porting Java

2005-12-20 16:04 • by ishmaeel
Well, at least this one is easily fixed. What if he had redirected the output of ls to a text file and parsed the file to retrieve the contents of a folder? I don't think this programmer is being the Prince of Darkness, so a mild "What The Heck" should suffice.

Re: Porting Java

2005-12-20 16:07 • by dobblego
54024 in reply to 54022
Suck My Lisp:
RevMike:

java.util.SortedSet no good for you?

snip



But I'd almost be willing to bet that, portability aside, spawning off
the Unix "sort" command to sort a large file would be a great deal
faster then doing it in Java.




How much?
I need some money - Christmas is killing me.

Re: Porting Java

2005-12-20 16:08 • by Suck My Lisp
54025 in reply to 54022
Oh, another problem with SortedSet (and the classes that implement it)
is that it might be perfectly OK for his file to have multiple
identical lines, which a Set-implementing class would clobber.

Re: Porting Java

2005-12-20 16:16 • by RevMike
54029 in reply to 54025
Suck My Lisp:
Oh, another problem with SortedSet (and the classes that implement it)
is that it might be perfectly OK for his file to have multiple
identical lines, which a Set-implementing class would clobber.


From the post to which I replied...
Anonymous:
I've done something like this before.....

I needed to do a sort unique on a hundred odd meg file.


How often does one do a 'sort unique' where multiple identical lines are perfectly ok?

Re: Porting Java

2005-12-20 16:21 • by Suck My Lisp
54030 in reply to 54029

How often does one do a 'sort unique' where multiple identical lines are perfectly ok?


As often as one uses an interface to sort a file. ;)



OK, didn't see that line.

Re: Porting Java

2005-12-20 16:33 • by Suck My Lisp
54034 in reply to 54024
dobblego:

How much?
I need some money - Christmas is killing me.


I think it would depend on the size of the file and exactly how you
implemented it in Java.  If you went with a quick & dirty
implementation using java.io.* and TreeSet, and you were only sorting
and uniqueing the file (and not loading into some data structure in the
JVM)  I stand by my guess that Unix sort would likely be
faster.  Shouldn't be hard to test...

Re: Porting Java

2005-12-20 16:33 • by Anonymous Coward
54035 in reply to 54001

kipthegreat:
ammoQ:

Sidenote: mkdir -p creates all the necessary directories on the way.
Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.


Presumably, the original author wrote more than just these lines in Java.  So it's not like they were unfamiliar with the language and how to use the javadoc to find something you're looking for..  I mean, they had to lookup Runtime.getRuntime().exec(), File.mkdirs() doesn't seem much harder...


You would think the person who shelled out to execute the mkdir command would be able to find File.mkdir. But I think another WTF here is W(hy)TF would Sun create a method names mkdir (or mkdirs)??? Wouldn't following their own conventions require the method name to be: makeDirectory() or makeDirectories() ? or something similar. I don't think you should have to read past the method name in the docs to get a high level understanding of what the method did. (Assuming you are coming from a non-unix world and are not familiar with mkdir)

Re: Porting Java

2005-12-20 16:40 • by RevMike
54036 in reply to 54030
Suck My Lisp:

How often does one do a 'sort unique' where multiple identical lines are perfectly ok?


As often as one uses an interface to sort a file. ;)



OK, didn't see that line.



Ha-Ha!

Actually, a class implementing SortedSet could work fine for identical records.  A primary key could be added to each record in order to provide uniqueness, but the Comparator is free to only use that key to perform arbitrary distinction amongst equivalent records when doing the sorting.

Re: Porting Java

2005-12-20 16:43 • by RevMike
54037 in reply to 54035
Anonymous:

kipthegreat:
ammoQ:

Sidenote: mkdir -p creates all the necessary directories on the way.
Anyway, I've just checked the docs and IMO it's pretty hard to find the right way, because of its name. (For those who wonder, it's File.mkdirs(). I've looked for something in the line of createDirecory() first. No wonder people are tempted to go the easy WTF way.


Presumably, the original author wrote more than just these lines in Java.  So it's not like they were unfamiliar with the language and how to use the javadoc to find something you're looking for..  I mean, they had to lookup Runtime.getRuntime().exec(), File.mkdirs() doesn't seem much harder...


You would think the person who shelled out to execute the mkdir command would be able to find File.mkdir. But I think another WTF here is W(hy)TF would Sun create a method names mkdir (or mkdirs)??? Wouldn't following their own conventions require the method name to be: makeDirectory() or makeDirectories() ? or something similar. I don't think you should have to read past the method name in the docs to get a high level understanding of what the method did. (Assuming you are coming from a non-unix world and are not familiar with mkdir)



Since the DOS/Windows command is also mkdir, I don't see much of an issue.  It does put out the VMS user who want File.CREATE /DIR(), but they're already pissed that there is no File.PURGE() method.

Re: Porting Java

2005-12-20 16:59 • by Dave_NYC
54038 in reply to 54009
RevMike:
Anonymous:
I've done something like this before.....



I needed to do a sort unique on a hundred odd meg file.


Isn't that what Perl hashes are for?

I've actually done this for some "quick & dirty" tasks, and it worked fine.


This is a great trick for moderately sized files. Memory constraints become a problem, of course, since the hash is completely in memory. If you run into this problem, try it with a hash TIE'd to gdmb or some other file-based medium. It's  slower, but at least your program won't die. SQLite is also a  handy option.

I'm sure you're aware, but I mention this for the sake of less seasoned perl coders on the list.
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment