|
I didn't see one of these in the generic thread, so I guess it's just a matter of time. Here's a thread for small questions relating to everyone's favourite programming language, Java. Chuck your questions up here. Attention! Java is not JavaScript Despite their similar names the languages are hugely different; questions relating to JavaScript should be directed to the Web Development Megathread I'm going to follow this with a post on IDEs/tools/etc. Anyone who wants stuff added to that post or contact me or whatever. zootm fucked around with this message at 18:57 on May 21, 2008 |
# ¿ Feb 23, 2008 11:11 |
|
|
# ¿ Apr 27, 2024 08:51 |
|
Hefty information post Here's the promised post on tools. IDEs A quick note on IDEs: Java is very verbose, so autocompletion is handy when working on it for any period of time. Luckily Java has some really great IDEs handy: NetBeans IDE - Open source, developed primarily by Sun. Gets a lot of features based around whatever Sun is currently pushing, uses Ant as its package format (meaning it's easy to run your build remotely) and has a really nice GUI editor, along with excellent support for Ruby on Rails should you decide to run, screaming, from Java at some point. Also comes with a fairly good profiler which I would recommend more highly if it hadn't crashed and burned when I was trying to profile something over a transatlantic link the other day. Eclipse - Another open source IDE, a little bit more flexible than NetBeans, but does more stuff for you. This was clearly better than NetBeans for a long time so this is what most teams use these days. Has a huge wealth of plugins (of variable quality) for the various frameworks available for Java. Also Bubblegum Wishes recommends this series of videos teaching Java from the ground up using Eclipse. IntelliJ IDEA - Costs money, but pretty much nobody who tries it ever goes back. I've not used it, but I don't think I've ever heard a negative review. Another note, though - it might be worth learning Java without these tools. They do a lot for you, and you will miss out on a lot of the subtleties of how Java works. Finding problems Debuggers and profilers (all of the tools above have at least the former), as with most languages, can be invaluable in finding Java problems. It's always worth knowing how your dependencies work, too, since it's the cause of a lot of misconceptions. Preventing problems Another tip is to use FindBugs to find common errors in your code. It may give you a lot of "false" positives, but if you keep your code clean you get a much nicer protection against the dumb mistakes that we all make sometimes than you get from "it compiles". I've run this ever since spending 2 hours with a workmate trying to find the cause of weird behaviour which turned out to be caused by someone doing == comparison on a boxed integer value ( this doesn't do what it looks like it does). For those of you who like to be doubly paranoid, PMD does this sort of job too. PMD works on source code (rather than compiled bytecode) though, which makes it a whole bunch easier to, among other things, add your own rules. This gives you the freedom to add a check on something you know you do a lot, which is handy. It's probably for more-advanced or -paranoid users to run both, though, and FindBugs will likely be easier and more productive if you only want to use one. Also, run your compiler with the "-Xlint" flag. This turns on all the warnings. This is a good thing. If you look at your code and see a warning that you decide is unwarranted (sadly there are some things that just don't work well with generics without warnings), you can add the @SuppressWarnings( "warningType" ) annotation to the method in questions, but do not do this until you are confident you understand the reason for the warning. Your code should be clean of warnings from both the compiler and FindBugs, unless you have a drat good reason to the contrary. zootm fucked around with this message at 13:26 on Jul 15, 2009 |
# ¿ Feb 23, 2008 11:19 |
|
csammis posted:Can one use IntelliJ or Netbeans if they're developing Java RCP applications that target the Eclipse framework itself, or does that lock one into having to use Eclipse? I'm doing this at work, but I can't friggin' stand Eclipse. It's slower than an rear end in a hat.
|
# ¿ Feb 24, 2008 22:31 |
|
Belgarath posted:Not strictly a Java question, but, does anyone know if Eclipse has anything similar to visual studio's #region ? code:
Incidentally this feature is available by default on Netbeans which uses an XML syntax like this: code:
|
# ¿ Feb 25, 2008 18:43 |
|
I'm not sure about JCreator since I've never used it, and the other ones are basically like I noted in the second post of this thread; is there anything specific you'd like to know?
|
# ¿ Feb 25, 2008 23:53 |
|
rotor posted:You won't be able to write to it, IIRC. Leehro posted:Does anyone have a quick tutorial or example for one of the EE persistence APIs? Everything I come across seems to be part of some framework. Other than that the Javadoc for the API is actually pretty good.
|
# ¿ Mar 1, 2008 10:37 |
|
Alan Greenspan posted:http://forum.java.sun.com/thread.jspa?threadID=300557&start=45&tstart=-1
|
# ¿ Mar 4, 2008 17:41 |
|
FuzzyBuddha posted:Ok, a bit late but... I want to thank you for this suggestion, if for no other reason than it tags errors as I type. I'm a horrible typer and this has already saved a lot of headache.
|
# ¿ Mar 5, 2008 00:20 |
|
Yeah, using generics at runtime is pretty much a dead end in the current implementation. It's just a compiler restraint (so you can, for example, get access to stuff like that by using an annotation processor, but that's unlikely to be what you want).
|
# ¿ Mar 18, 2008 08:41 |
|
ReaperUnreal posted:The java doc is of no help quote:Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header. You're causing deadlock by waiting for input before it's been produced in the same thread. zootm fucked around with this message at 10:13 on Mar 21, 2008 |
# ¿ Mar 21, 2008 10:10 |
|
ReaperUnreal posted:Huh, how did I not see that? Thanks for the help, looks like I'll be reorganizing things. This'll be fun, having to create input and output streams inside a loop. Honestly, some times, I wish I was using C instead of Java. nik9000 posted:Once you learn Ant, learn Maven. Its better because its more than a build tool, its a project management tool. zootm fucked around with this message at 18:57 on Mar 21, 2008 |
# ¿ Mar 21, 2008 18:55 |
|
Brain Candy posted:Immutable objects definitely nice for many things, but they come at a price. Strings, for example are immutable and the JVM does all sorts of optimization with them. But you'll also notice that there are special helper classes, StringBuilder and StringBuffer, just to create new Strings. In general, every time you change an immutable field you have to create a new object.
|
# ¿ Mar 25, 2008 14:20 |
|
It entirely depends on what you want to do, and what your requirements are. Java has support for pretty much every method of parsing XML out there. DOM allows you to traverse and manipulate an XML document as an object representing its structure. SAX and StAX are stream parsers which scale well but essentially mean you have to maintain any state that you need yourself. StAX is a more modern "pull" parser which is a lot easier to use. JAXB allows you to bind structured XML documents to and from corresponding basic "bean" objects automatically. XPath allows you to query a loaded XML document for a given "path" allowing you to quickly query parts of the document.
|
# ¿ Mar 26, 2008 14:56 |
|
Fehler posted:Thanks, but I thought Swing and SWT were different things. Can I actually use Swing classes together with SWT? As regards the SWT-and-extra-software thing, SWT does require extra stuff to be bundled per-platform since it has native components. Swing does not because it comes with the JRE.
|
# ¿ Mar 30, 2008 22:43 |
|
Fehler posted:It seems like the whole bundling of native software for SWT would be a bit annoying, so do you think I should just move to Swing? It's not like I put much thought into what to use when I started... Fehler posted:Are there any good Swing tutorials out there that explain the basics for somebody who knows nothing about Java GUIs? Fehler posted:Also, is it correct that Swing is the one with that ugly blue-silverish GUI? Any way around that?
|
# ¿ Mar 30, 2008 23:25 |
|
"start" ignores input. Your Java code is working fine, but start spawns and starts a new console window for you and that's all it does. What is it you're trying to do? There's bound to be a better way to do this?
|
# ¿ Apr 2, 2008 16:03 |
|
That would be a better idea, yeah. If you could invoke something like tail -f (I realise that's not a Windows program) it'd also follow the output of a log file. If you're just wanting to catch logging, however, use a standard logging toolkit like log4j or java.util.logging or Commons Logging or whatever and write a log "target" for that which just outputs the logging information to a window or whatever you like. There's no good reason not to use "proper" logging and doing this right will give you a lot more freedom if this is important to you. Popping up an "echo" window natively doesn't seem like a good solution. zootm fucked around with this message at 17:45 on Apr 2, 2008 |
# ¿ Apr 2, 2008 17:43 |
|
epswing posted:Yep, I'm using log4j extensively, this is just to present the user with some visual feedback that what MyApp is running is actually running.
|
# ¿ Apr 2, 2008 22:02 |
|
epswing posted:What do you mean by "Just capture the logging information directly"? I'm spawning a process, how else do you propose I capture it's stdout/err? Ah, I had thought you were running it in the same JVM, presumably you've a reason for not doing so. Just capture in and out, yeah.
|
# ¿ Apr 3, 2008 07:34 |
|
ColdPie posted:Basically a utility function that compares two strings in different ways based on the bitflags. The code's for work, so I don't have it on me, but the flags are something like COMP_CASE_INSENSITIVE, COMP_BEGINS_WITH, COMP_ENDS_WITH, COMP_CONTAINS and so on. It's a whole lot easier to maintain one function and a couple bitflags than a few dozen functions with various combinations of the above options. The functions signature is essentially static void compare(String expected, String found, int flags).
|
# ¿ Apr 9, 2008 07:25 |
|
TRex EaterofCars posted:The VM will mark it collectible after its containing block's scope ends unless it's referenced somewhere else currently in scope or static, like a Map or a List of some kind. Note that it's just marked for collection, the actual collection happens at some other point. TRex EaterofCars posted:Like you said, you can set it to null manually and call System.gc(); but absolutely no guarantees are made about when the memory consumed by that class will be reclaimed, if ever. adante posted:Actually I cannot get the key or list of keys either (maybe you can, if so please share how ).
|
# ¿ Apr 20, 2008 13:54 |
|
BELL END posted:I was under the impression that HashMaps don't allow more than one value to be stored with the same key, each subsequent call to put(k) overwrites what is already at k. quote:Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. Edit: Also TRex's suggestion is the "traditional" way of doing this, and will work if you don't want to pull a JAR dependency. It's just a little bit more work to deal with the collection yourself.
|
# ¿ Apr 23, 2008 17:58 |
|
TRex EaterofCars posted:Well I'll be goddamned. It must be a side effect of working in an environment devoid of intellectual curiosity but I never even bothered to check Commons for something like this. Thanks. Glad it's helped you too
|
# ¿ Apr 23, 2008 19:46 |
|
You can code with it fine in Netbeans, just reference the JAR and the libraries that it requires. The Netbeans visual designer is based on Swing and it's very unlikely you'll find an SWT designer for Netbeans, though.
|
# ¿ Apr 24, 2008 09:31 |
|
Drumstick posted:Component2 will show, but component wont show up at all. By themeselves, they will be up and working just fine, just not together... With some layouts you need to pass the "position" where the item should appear as well - with BorderLayout you can choose NORTH, SOUTH, EAST, WEST, or CENTER, for example.
|
# ¿ Apr 25, 2008 07:31 |
|
Drumstick posted:okay thanks! i will look into that right away. I havent had to add two components into a frame before, thanks again
|
# ¿ Apr 25, 2008 08:04 |
|
Bonus posted:The other day I was thinking, is there any reason why Java doesn't have return type polymoprhism? I mean like having public String foo() and public int foo() and then the one that gets called depends on what kind of type of return value the caller expects. This works really nicely in languages like Haskell and I see nothing in Java that prevents it form having this feature. code:
code:
code:
|
# ¿ Apr 30, 2008 14:22 |
|
I'm fairly sure you cannot do this in Scala, for what it's worth. I'll have a look tonight if I remember though. Also the fact that you can do it in Jaskell does not mean that the bytecode or the JVM supports it directly. A lot of JVM languages encode such concepts (the multiple inheritance in Scala, for example, is not something that JVM bytecode supports directly).
|
# ¿ Apr 30, 2008 15:56 |
|
TRex EaterofCars posted:I understand what you mean, but the JVM docs specifically state that it does not restrict overloaded method returns.
|
# ¿ Apr 30, 2008 19:43 |
|
TRex EaterofCars posted:I actually do not know how Jaskell is implemented. I kinda wrote that post quickly and should have put the word "also" in there somewhere. :p
|
# ¿ Apr 30, 2008 20:41 |
|
Fehler posted:How do I save an XML document I created with org.w3c.dom.Document to a file? Flying somewhat blind: code:
|
# ¿ May 2, 2008 19:57 |
|
JingleBells posted:The JAXP tutorial has the following example: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPXSLT4.html I guess it's worth noting that that does require JAXP, which isn't standard in Java until I think Java 5 or 6. zootm fucked around with this message at 12:21 on May 3, 2008 |
# ¿ May 3, 2008 12:16 |
|
Fehler posted:Looks like it works, thanks! I guess something like doc.save(path) would have been too easy...
|
# ¿ May 3, 2008 14:39 |
|
Did you remove the absolute path link to the library in question? That could cause it problems. I've certainly gotten relative paths to work fine without error markers many times before so it's not something that can't be fixed.
|
# ¿ May 8, 2008 00:26 |
|
adante posted:Howdy, if I have some Class<?> object (in my case, returned from a Field#getType() during reflection), how can I tell if the class represented by this object implements a particular interface or class? I guess this is the equivalent to the instanceof operator, except for the class type and not an object. So something like the following, based on your example. code:
|
# ¿ May 14, 2008 17:31 |
|
I'm pretty sure that can't be done. And I don't think it's something that would be good to have in code anyone would use in any case.
|
# ¿ May 14, 2008 18:59 |
|
BELL END posted:Looks like it's a javascript question and he's in the wrong thread (apologies if I'm wrong!) it's happened a few times in this thread already. Maybe zootm could add a quick "Java != Javascript" and a link to the web development thread to the OP? I'm interested to know what language the stuff within the <%= %>-delimited block is; I know Rails uses that but I don't think & concatenates strings in Ruby. The only think I could think of which would be used for web programming is VB?
|
# ¿ May 21, 2008 19:01 |
|
Twitchy posted:You are looping over all the input / output in the seperate threads, but updating them all on the Event Dispatch thread (i.e. SwingUtilities.invokeLater(), which is the same no matter what JFrame you're using). Because you're looping over and invoking the Event Dispatch thread constantly it's locking up the other GUI. Could you not invoke the append() method at the end, and save the looping part in a StringBuilder or something? Furthermore the invokeLater method just appends update events to the queue, unless there was a lot of events (which the last post shows that there really isn't) you can't really block the UI thread like that. And even then I don't think the effect would be the one shown there. epswing: I'm still worried that the stream reader stuff is running in the UI thread for some reason (the symptoms fit too well, dammit). Can you breakpoint on it (any line) in a debugger and check the full stacktrace doesn't have any Swing stuff in it?
|
# ¿ Jun 4, 2008 09:18 |
|
Twitchy posted:Ah, it seems zootm is probably right, how are you making the threads? Twitchy posted:Instead of doing a proper debug if you want the easy way to find out just do
|
# ¿ Jun 4, 2008 12:53 |
|
|
# ¿ Apr 27, 2024 08:51 |
|
epswing posted:The thread creating the ProcessBuilder and both threads sucking up with stdout/err are isEventDispatchThread() == true. epswing posted:I loving hate GUI/Swing work. This is the kind of stuff that makes me want to quit my job and go into pig farming. It's challenging, but for all the wrong reasons.
|
# ¿ Jun 4, 2008 17:42 |