Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

The problem I have with BlueJ is that, unless you really have problems grasping object oriented fundamentals, it doesn't offer much that other IDEs don't while teaching you some processes that don't really exist outside of BlueJ.

That's not to say it's a bad program (I learned on it back in my first CS course) but I would just be aware that there really isn't anything like it in wide use.

Adbot
ADBOT LOVES YOU

Hand Model
May 13, 2013

carry on then posted:

Are you creating a new project for each problem? If not, it thinks the new file you've started is going to be called from some other file. In Netbeans you can right-click on the file and choose Run, as long as it has a public static void main method.

It would probably be worthwhile teaching yourself how to compile and run java from the command line, as an IDE is probably overkill if you're just learning.


Oh, no I was creating new files within the same project. And yeah that works. Thanks!

I think compiling isn't too hard, but an IDE looks so much nicer! But fine, you're right, I should just practice doing everything.

I'm poo poo with computers, unfortunately.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Hand Model posted:

Oh, no I was creating new files within the same project. And yeah that works. Thanks!

I think compiling isn't too hard, but an IDE looks so much nicer! But fine, you're right, I should just practice doing everything.

I'm poo poo with computers, unfortunately.

I'm not saying you should drop Netbeans right now if it works for you, more that it sounds like you're working with smaller problems, and something as monolithic as Netbeans can seem to be in your way at times.

Hand Model
May 13, 2013

carry on then posted:

I'm not saying you should drop Netbeans right now if it works for you, more that it sounds like you're working with smaller problems, and something as monolithic as Netbeans can seem to be in your way at times.

I agree that I am having some dumb and frustrating problems in it. And that's a waste of time.

But then I went to the command line and it's not much better haha

Mouth Ze Dong
Jan 2, 2005

Aint no thing like me, 'cept me.
I use an IDE for its auto complete features and code highlighting and set up javac.exe as a default program option (right click > open with > javac) for easy compiling.
If it doesn't compile, I bring up the command prompt to see the error, but the IDE is usually good about highlighting my errors before I get to that point.

mister_gosh
May 24, 2002

What does this mean?

code:
public String toString() {
   return getFoo() + (isBar() ? " *" : "");
}
getFoo() returns a string, but what is it appending and how?

If isBar is true, it...adds a space and a literal asterisk? Otherwise it adds nothing? In other words:

(boolean test) ? (if true do this) : (otherwise do this)

The syntax just seems confusing and I'm not quite sure how to google it.

Glimm
Jul 27, 2005

Time is only gonna pass you by

mister_gosh posted:

What does this mean?

code:
public String toString() {
   return getFoo() + (isBar() ? " *" : "");
}
getFoo() returns a string, but what is it appending and how?

If isBar is true, it...adds a space and a literal asterisk? Otherwise it adds nothing? In other words:

(boolean test) ? (if true do this) : (otherwise do this)

The syntax just seems confusing and I'm not quite sure how to google it.

It's known as the ternary operator:

http://java.about.com/b/2009/06/06/java-term-of-the-week-ternary-operator.htm

Mouth Ze Dong
Jan 2, 2005

Aint no thing like me, 'cept me.

The ending parenthesis should be after the boolean test, but yes, that's the jist of it.

code:
private int foo = (5>=17)? 11 : 17;

or

return getFoo() + (isBar())? " *" : "";
You'll have to find the getFoo() method to find what it's returning.

http://www.ibiblio.org/java/course/week2/43.html

DholmbladRU
May 4, 2006
Been creating a screen scraper for a few web pages in java. I am using jsoup to parse some html after I get back the pages. In general I can iterate through multiple pages on the site using some URL parameter which the page is using. However I have hit a speed bump with a specific website I am attempting to scrape, they seem to be using ajax to update the page. I am unsure how I will invoke the 'button' which is going to the next page. I assume it is ajax because the url does not change. Does anyone have any insight on this?

Tesseraction
Apr 5, 2009

Well you could look at the page and figure out how the onclick method fetches the next page, or you can ask Stack Overflow and get suggested to use this library to provide what you need. You still need to inspect the page you're scraping to know what its 'next page' button is, though.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

DholmbladRU posted:

Been creating a screen scraper for a few web pages in java. I am using jsoup to parse some html after I get back the pages. In general I can iterate through multiple pages on the site using some URL parameter which the page is using. However I have hit a speed bump with a specific website I am attempting to scrape, they seem to be using ajax to update the page. I am unsure how I will invoke the 'button' which is going to the next page. I assume it is ajax because the url does not change. Does anyone have any insight on this?

JSoup doesn't really support AJAX pages IIRC. When I ran into this problem a while ago I ended up looking into the Javascript. Once I pieced together the call to the external service that was being made I manually called the exposed service. From there you will need to figure out how to process the data as it could return HTML, JSON, XML or whatever.

If it returns HTML just parse it with JSoup, if it returns JSON I would plug into GSON or your favorite Java JSON library, and XML I usually work with the built in DOM parser because I'm to lazy to learn a better XML library.

DholmbladRU
May 4, 2006

lamentable dustman posted:

JSoup doesn't really support AJAX pages IIRC. When I ran into this problem a while ago I ended up looking into the Javascript. Once I pieced together the call to the external service that was being made I manually called the exposed service. From there you will need to figure out how to process the data as it could return HTML, JSON, XML or whatever.

If it returns HTML just parse it with JSoup, if it returns JSON I would plug into GSON or your favorite Java JSON library, and XML I usually work with the built in DOM parser because I'm to lazy to learn a better XML library.

Thanks for the information. I think that HtmlUnit will support the javascript calls to iterate through the ajax pages. Once I perform that maybe I could use jsoup to parse through the html. Any suggestions on how to find the javascript call attached to this button?

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Use firebug plugin for firefox. Inspect the button and hopefully you can trace the function that gets called.

Tesseraction
Apr 5, 2009

If you're working entirely 'headless' (not strictly the definition, but if you're working straight from a terminal interface then y'know), you might have to use heuristic guesses of searching through (X)HTML tags for something suggesting the word 'next (page)' and then looking around that for an onclick method.

Horse Cock Johnson
Feb 11, 2005

Speed has everything to do with it. You see, the speed of the bottom informs the top how much pressure he's supposed to apply. Speed's the name of the game.
Can anyone recommend some good books or other resources for a long time C# developer looking to gain a deeper understanding of Java?

I've been a .NET programmer for >5 years and am pretty comfortable with Java at a high level. I've recently started a new job where I'll need to get up to speed with Java and am looking to expand my knowledge. I don't need anything that starts off with "Hello world!" or "What is a class?" but I've never really done anything with Java outside of a classroom either. I'm looking for stuff similar to CLR via C# but for Java and the JVM - something that explores some more advanced topics like threading, concurrency, performance, what's going on under the hood, etc.

Everything I find on Amazon is either a basic intro to Java or seems pretty outdated. For example: Java Concurrency in Practice - this sounds like it would be useful but it's 7 years old. Is it still relevant?

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Horse Cock Johnson posted:

Can anyone recommend some good books or other resources for a long time C# developer looking to gain a deeper understanding of Java?

I've been a .NET programmer for >5 years and am pretty comfortable with Java at a high level. I've recently started a new job where I'll need to get up to speed with Java and am looking to expand my knowledge. I don't need anything that starts off with "Hello world!" or "What is a class?" but I've never really done anything with Java outside of a classroom either. I'm looking for stuff similar to CLR via C# but for Java and the JVM - something that explores some more advanced topics like threading, concurrency, performance, what's going on under the hood, etc.

Everything I find on Amazon is either a basic intro to Java or seems pretty outdated. For example: Java Concurrency in Practice - this sounds like it would be useful but it's 7 years old. Is it still relevant?

Concurrency in Practice is a very good java book. I'm also a fan of Effective Java. Despite being 7 and 5 years old, they're still pretty relevant. The latest Java Version is Java SE 7, out in 2011, which... doesn't really contain a lot that's super interesting. Just some convenience stuff - Strings in switch statements, ways to represent binary literal integers, etc. Java 6, out in 2006, was the last big change, and Java 8 (2014?) is going to have a lot of convenience stuff for lamdas and functional-ish programming, which will be great to work with.

It has been my experience, starting out with Java about a year ago, that familiarizing myself with the Apache Commons and Google Guava libraries has been incredibly useful. They contain the things that should be in core Java but aren't.

Gravity Pike fucked around with this message at 21:44 on Jul 29, 2013

Stabbey_the_Clown
Sep 21, 2002

Are... are you quite sure you really want to say that?
Taco Defender
This is probably a dumb question, but doing a google search hasn't helped me understand.

I'm making a new servlet project in Eclipse (well, okay a "Hello World" thing). I'm using the New Dynamic Web Project wizard, and the third page of the wizard is Web Module, where there are two options: Context Root and Content Directory.

I've learned that context root is the root directory, so if should leave it as "\" if I want it to look in the "E:\Tomcat7\webapps\ROOT\...".

I'm not quite sure what the Content Directory is, what it is looking for there. Google says that it "serves files from there". I don't know what it wants.

Thanks for helping.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
Question about ConcurrentModificationException, but may be unexpected:

Say, I have a list, and go over it with one iterator that duplicates entries, and with another iterator that selectively deletes entries. I imagine that 'undetermined behavior' here would be not knowing whether there has been duplication or deletion. If so, I think that could be desired behavior for some hypothetical case. If I'm correct, I could just catch the exception and continue. Or am I incorrect and there could be some other consequences?

Amarkov
Jun 21, 2010

supermikhail posted:

Question about ConcurrentModificationException, but may be unexpected:

Say, I have a list, and go over it with one iterator that duplicates entries, and with another iterator that selectively deletes entries. I imagine that 'undetermined behavior' here would be not knowing whether there has been duplication or deletion. If so, I think that could be desired behavior for some hypothetical case. If I'm correct, I could just catch the exception and continue. Or am I incorrect and there could be some other consequences?

The Iterator class does not specify the precise mechanism of iteration. The iterator might retrieve one element from the collection at a time. It might retrieve elements from the collection in size 5 blocks. It might make its own internal copy of the collection and do something fiddly to keep it synchronized. So if you try to modify the list with two iterators at the same time, you have absolutely no guarantee as to what will happen; it's not as simple as "oh well they might both hit the same element at the same time".

In any case, there is also no guarantee that a ConcurrentModificationException will be thrown, so you cannot rely on catching it for your program to function correctly.

Amarkov fucked around with this message at 18:37 on Jul 30, 2013

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I would consider my program to function correctly if it continued working despite all (whether by catching runtime exceptions or missing some glitches), and didn't break my computer. :) Or run out of memory by accident... Oh, I just will have to test it out. Have to. :chef:

Amarkov
Jun 21, 2010
I mean, you can always make the program keep running by doing

code:
try {
    ...
} catch (Throwable t) {
    //nop
}
But you have no guarantees about what the list will look like when you're done, or even which of the operations actually worked. If you don't care what the results of your modifications are, why are you doing them?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
If you can keep a secret, I had this as part of an idea of artificial evolution while commuting. /secret.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

supermikhail posted:

If you can keep a secret, I had this as part of an idea of artificial evolution while commuting. /secret.

I think it's deterministic, just not guaranteed in any given circumstance.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Amarkov posted:

code:
try {
    ...
} catch (Throwable t) {
    //nop
}

Generally, you're going to want to catch Exception instead of Throwable, unless you're able to handle things like OutOfMemoryError or StackOverflowError. Errors are generally unrecoverable, and your best bet is to let your program bail.

Amarkov
Jun 21, 2010

Gravity Pike posted:

Generally, you're going to want to catch Exception instead of Throwable, unless you're able to handle things like OutOfMemoryError or StackOverflowError. Errors are generally unrecoverable, and your best bet is to let your program bail.

I recover from them by running the program on a remote supercomputer :colbert:


e:

trex eaterofcadrs posted:

I think it's deterministic, just not guaranteed in any given circumstance.

Yes, this is correct. I don't know how it's actually done, but the obvious fail-fast implementation has completely uninteresting behavior; the first iterator will simply stop working whenever a modification is made using the second iterator.

Amarkov fucked around with this message at 02:09 on Jul 31, 2013

Max Facetime
Apr 18, 2009

Gravity Pike posted:

Generally, you're going to want to catch Exception instead of Throwable, unless you're able to handle things like OutOfMemoryError or StackOverflowError. Errors are generally unrecoverable, and your best bet is to let your program bail.

Also VirtualMachineError and ThreadDeath, which is silently thrown and handled when Thread.stop() is called.

Rothon
Jan 4, 2012

Max Facetime posted:

Also VirtualMachineError and ThreadDeath, which is silently thrown and handled when Thread.stop() is called.

Using Thread.stop() is just about as much of a horror as catching Throwable.

Max Facetime
Apr 18, 2009

It's not nearly as bad as Thread.suspend() and Thread.resume() at least. Really it's the same thing as doing throw new ThreadDeath(); in your code.

Except you can also cause that to happen while someone else's code is executing. Sun's/Oracle's documentation makes a big deal about Objects being left in unknown states making their further use risky, but to my mind the important use is in a fast and reliable cleanup of resources that a plugin manager might perform. If the unknown code that's executing doesn't corrupt all the state when faced with a thrown OutOfMemoryError then it should survive a ThreadDeath too. E: well not survive per se, but you know what I mean.

For something like a plugin system it's not perfect but it's as good or better than the alternatives: making threads interruptible and/or relying on some custom shutdown procedure existing and working in a timely manner. I know I've written something like this a few times:
Java code:
private static void sleep(long millis) {
  try {
    Thread.sleep(millis);
  } catch(InterruptedException ignored) {}
}
Mainly because I don't know how to handle the interrupt so it fits within the larger context.

Max Facetime fucked around with this message at 23:34 on Jul 31, 2013

Brain Candy
May 18, 2006

Interrupts are everywhere 'cause spurious wakes are a real thing.

The key trick is every interruptible call really needs to matched with a loop+condition. If you really want to guarantee a 'sleep' for at least some time :

Java code:
private static void sleep(long millis) {
  long end = System.getCurrentTimeMillis() + millis;  
  long remaining;
  while ( (remaining = end - System.getCurrentTimeMillis()) > 0) {
    try
    {
      Thread.sleep(remaining); //This is sloppy because sleep generally isn't that accurate
    }
    catch (InterruptedException ie){} //Juggernaught, etc.
  }
}
Of course, the super secret sanity preserver is to use Executors in lieu of threads so you can actually cancel/stop things without special handling and let the interrupts work for you.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Brain Candy posted:

Java code:
private static void sleep(long millis) {
  long end = System.getCurrentTimeMillis() + millis;  
  long remaining;
  while ( (remaining = end - System.getCurrentTimeMillis()) > 0) {
    try
    {
      Thread.sleep(remaining); //This is sloppy because sleep generally isn't that accurate
    }
    catch (InterruptedException ie){} //Juggernaught, etc.
  }
}

... and at this point, I'm just going to use Uninterruptibles.sleepUninterruptibly(), because Guava includes all much of the functionality that really ought to be there in the first place. (Does anyone know if this has the same issues with time that Thread.sleep() does? It's always been Good Enough™ for me.)

Brain Candy
May 18, 2006

Looking at it, Guava still has the same problem because it's basically the loop I posted plus actually forwarding the interrupt. The heart of the issue is Thread.sleep(long,long). There are no guarantees about the minimum argument it consistently works for. For years, on windows (maybe still?), Thread.sleep(1) actually slept 10ms because that was the period of the available system timer. This isn't a big deal if you pass 1000ms into it, you've just got to be aware that the accuracy is going to be significantly less than the precision and that it's machine/jvm/os dependent.

Stabbey_the_Clown
Sep 21, 2002

Are... are you quite sure you really want to say that?
Taco Defender
Tomcat 7 says that it is started and running, but http://localhost:8080/ is giving me a (Tomcat-generated) 404. I swear this poo poo was working the other day. I have no idea why it isn't working now. Tomcat is clearly running, it's generating the 404 message, so why can I not see it (or any other html or jsp page) that are in the webapps\ROOT directory?

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Stabbey_the_Clown posted:

Tomcat 7 says that it is started and running, but http://localhost:8080/ is giving me a (Tomcat-generated) 404. I swear this poo poo was working the other day. I have no idea why it isn't working now. Tomcat is clearly running, it's generating the 404 message, so why can I not see it (or any other html or jsp page) that are in the webapps\ROOT directory?

For starters, try clearing the work folder in tomcat and make sure that webapps folder has permissions set correctly, toss a chmod -R 777 in there.

After that, any errors in the tomcat logs?

Also double check what the expected URL should be. Probably http://localhost:8080/<webapp foldername>/

The trailing slash can be important, if /<foldername> doesn't work makes sure you try /<foldername>/

Zaphod42 fucked around with this message at 15:58 on Aug 1, 2013

Stabbey_the_Clown
Sep 21, 2002

Are... are you quite sure you really want to say that?
Taco Defender
EDIT: Deleting the bad project thing seems to have fixed it I think.

I think chmod is Linux only. This is Windows, but the properties shows that I have full control over that directory.

It appears I screwed something up when making a stupid test project in Eclipse, because the only page that actually works is "http://localhost:8080/HelloWWW2". Wonderful. How do I un-screw it?

In the Eclipse project, I set the URL mappings to /HelloWWW2, and changed the content directory from \WebContent to \ROOT. Apparently something like that was the wrong thing to do.

I saw this in the catalina log:

"Aug 01, 2013 10:25:39 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWWW2' did not find a matching property."

The localhost access log shows everything coming up 404's except for HelloWWW2.

Stabbey_the_Clown fucked around with this message at 17:26 on Aug 1, 2013

Volguus
Mar 3, 2009

Stabbey_the_Clown posted:

EDIT: Deleting the bad project thing seems to have fixed it I think.

I think chmod is Linux only. This is Windows, but the properties shows that I have full control over that directory.

It appears I screwed something up when making a stupid test project in Eclipse, because the only page that actually works is "http://localhost:8080/HelloWWW2". Wonderful. How do I un-screw it?

In the Eclipse project, I set the URL mappings to /HelloWWW2, and changed the content directory from \WebContent to \ROOT. Apparently something like that was the wrong thing to do.

I saw this in the catalina log:

"Aug 01, 2013 10:25:39 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWWW2' did not find a matching property."

The localhost access log shows everything coming up 404's except for HelloWWW2.

Oh, that's probably a snafu from the Eclipse Tomcat plugin (or is it called the web plugin?). What I usually did when I had to run tomcat from Eclipse was to tell the tomcat server (In the servers view) that it should use the existing tomcat configuration and not make a copy for itself (which is what it does by default). You can change that in the properties page of the server. If you cannot (the radio buttons are disabled) just delete the server and re-add it.

Whenever it messes up I usually just remove/re-add the server information. Dunno if it's the best way, it usually worked for me.

Since you're working on a test project ... you're probably ok to remove the project as well and just make a new one.

DholmbladRU
May 4, 2006
I am attempting to create a jsp website. I have some functionality working, however I am attempting to port over some stand alone code into my application. This code references external jar files which have been imported into the web project in eclipse. However I keep getting a class not found error when I execute my method. Do I need to perform <%@page import="com.class"%> on external jar files which are found under 'Referenced Libraries' ? Let me know if this is unclear


edit:nevermind. they need to be in the lib directory...

DholmbladRU fucked around with this message at 20:58 on Aug 1, 2013

FateFree
Nov 14, 2003

I need to collect metrics in memory and occasionally flush the list contents to the database. What's the best data structure to use that will be threadsafe while metrics are being collected, but that I can also switch out to dump the ones in memory to the database in a timer thread?

Should I just use Collections.synchronizedList, and then every now and then swap references from a live list to a secondary list so metrics can be collected while the dumping is going on? Is there any simple pattern for doing this?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
I think that a ConcurrentLinkedQueue will do just what you want without having to use a Collections.synchronizedList. However I don't think your approach to switching out the references is the correct way to do it.

I suggest that the dumping job immediately creates a copy of the queue, then does the inserts and finally tells the queue to removeAll elements in your copy. If no one else but this dump job is reading the queue then this should be safe.

Another possible more light-weight approach is for the dump job to create an iterator and then for each element, removes and insert it / batch it into your bulk insert. This however might not terminate if the iterator reflects new items that were added to the queue after its creation, which the javadoc doesn't spell out.

quote:

The returned iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

FateFree
Nov 14, 2003

Hard NOP Life posted:

I think that a ConcurrentLinkedQueue will do just what you want without having to use a Collections.synchronizedList. However I don't think your approach to switching out the references is the correct way to do it.

I suggest that the dumping job immediately creates a copy of the queue, then does the inserts and finally tells the queue to removeAll elements in your copy. If no one else but this dump job is reading the queue then this should be safe.

Another possible more light-weight approach is for the dump job to create an iterator and then for each element, removes and insert it / batch it into your bulk insert. This however might not terminate if the iterator reflects new items that were added to the queue after its creation, which the javadoc doesn't spell out.

Hmm thanks lets see... the javadoc makes it sound like it may get new elements in which case I'd have the same concern about the job never ending.

However in your first suggestion I like the fact that removeAll isn't called until the end of the dump job - if I do the dump in one transaction and it fails, its nice that those elements won't be lost and the next job can get them all. To do the copy should I just pass the original list into a ConcurrentLinkedQueue(collection) constructor? If so this should work out pretty well, I appreciate it.

edit - well this turned out pretty easy:

code:
private ConcurrentLinkedQueue<Metric> queue = new ConcurrentLinkedQueue<>();
      
@Override
public void add(Metric metric) {
    queue.add(metric);
}

@Override
public void flush() {
   List<Metric> metrics = new ArrayList<>(queue);
   save(metrics);
   queue.removeAll(metrics);
}

FateFree fucked around with this message at 00:46 on Aug 7, 2013

Adbot
ADBOT LOVES YOU

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
Can't you just use .drainTo() on the queue? Just drain the queue to a new list, and it's atomic.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply