• Ekevu (unregistered)

    Ah!, the wonderful benefits of knowing the system you're running on! :-)

  • (cs)

    Haha, that's hilarious. Doesn't even check the os to see if it's unix first.

  • (cs) in reply to whojoedaddy

    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.

  • Jim (unregistered)

    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?  

  • (cs) in reply to whojoedaddy
    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.
  • aprenot (unregistered)

    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. 

  • coward (unregistered) in reply to aprenot

    might as well have writen a short shell script.

  • Anonymous Coward (unregistered)

    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".
  • (cs)

    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.

  • (cs) in reply to coward

    This kind of stuff happens a lot in PERL too.

    but sometimes its just so easy to

    cat tmpfile| sort

  • JavaBean (unregistered)

    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!

  • (cs)

    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]

  • (cs)
    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.

  • boohiss (unregistered)

    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.

  • (cs) in reply to boohiss

    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?

  • (cs)

    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.

  • (cs) in reply to Volmarias

    I just reread my post. Need more coffee for lucidity :(

  • (cs)
    Alex Papadimoulis:

    <font color="#000099">public static</font> MultipartRequest createMultipartRequest (HttpServletRequest req) 
    <font color="#000099">throws</font> IOException
    {
    [SNIP]

    <font color="#006600">// Create a temp directory for the attachments</font> Runtime.getRuntime().exec (<font color="#990000">"mkdir -p "</font> + 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:

    <font color="#000099">public static</font> MultipartRequest createMultipartRequest (HttpServletRequest req) 
    <font color="#000099">throws</font> IOException
    {
    [SNIP]

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

    [SNIP]

    }




  • (cs) in reply to ammoQ
    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...
  • Mike (unregistered)

    Maybe the original developer was told the app would only be run on Unix.

  • (cs) in reply to RevMike
    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.
  • Pedant (unregistered) in reply to Mike

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

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

  • (cs)

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

    --Rank

  • (cs) in reply to ammoQ
    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?
  • (cs) in reply to TheDauthi
    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!
  • Rodpheus (unregistered) in reply to RevMike
    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

  • (cs) in reply to Pedant
    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.
  • (cs) in reply to Rodpheus
    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!
  • (cs) in reply to kipthegreat
    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().
  • (cs) in reply to TankerJoe
    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.
  • (cs) in reply to ammoQ
    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.
  • (cs) in reply to boohiss

    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?

  • (cs) in reply to Pedant

    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.

  • Sanjeev (unregistered)

    <font size="2">I am sure Nick Smith has his own reasons to hate Jave if his piece of art does not work</font> :'(

  • (cs) in reply to ammoQ

    <font size="1" style="font-family: verdana;">

    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.



    <font size="2">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.

    </font>
    </font>
  • (cs) in reply to ammoQ

    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?

  • (cs) in reply to JohnO
    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?
  • Joe Van Dyk (unregistered) in reply to mlathe

    In Ruby, it's not too much more difficult than the shell version:

    File.readlines('tmpfile').sort

    Probably the same exists in Perl.

  • Warp (unregistered) in reply to Manni
    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)?
  • (cs) in reply to RevMike
    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.

  • (cs)

    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.

  • (cs) in reply to Suck My Lisp
    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.
  • (cs) in reply to 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.

  • (cs) in reply to Suck My Lisp
    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?
  • (cs) in reply to RevMike

    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. ;)

    <font size="1">OK, didn't see that line.</font>
  • (cs) in reply to dobblego
    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...
  • Anonymous Coward (unregistered) in reply to kipthegreat

    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)

  • (cs) in reply to Suck My Lisp
    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. ;)

    <font size="1">OK, didn't see that line.</font>


    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.
  • (cs) in reply to Anonymous Coward
    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.
  • Dave_NYC (unregistered) in reply to RevMike
    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.

Leave a comment on “Porting Java”

Log In or post as a guest

Replying to comment #:

« Return to Article