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
1337JiveTurkey
Feb 17, 2005

For the purposes of making a file uploader, a JNLP-launched applet would work. JNLP allows a slightly larger set of permissions than plain applets such as to create a file open dialog.

https://jdk6.dev.java.net/plugin2/jnlp/

Adbot
ADBOT LOVES YOU

lamentable dustman
Apr 13, 2007

🏆🏆🏆

zootm posted:

It's a pity to have to spin up a JVM for such a small use-case but as dvinnen points out it's something that browsers simply can't do.

HTML 5 (lol) should fix that when/if it ever comes out



1337JiveTurkey posted:

For the purposes of making a file uploader, a JNLP-launched applet would work. JNLP allows a slightly larger set of permissions than plain applets such as to create a file open dialog.

https://jdk6.dev.java.net/plugin2/jnlp/

You would still need to sign it but JNLP is cool and a good way to package an applet. Makes using extensions like Java3D a breeze.

lamentable dustman fucked around with this message at 19:13 on May 27, 2009

WalletBeef
Jun 11, 2005

A bit of backstory before I get to my question ...

I have recently been tasked with making a java application "better". This application doesn't seem to have any cpu bottlenecks but it uses LOTS of memory.

So I fire up the latest version of JProfiler and discover that Class X has MANY instances that take up about LOTS of memory. So I take a look at the source and Class X has a bunch of data members, one of which is a hashmap. The only reference to this hashmap is a .put call. The contents of the hashmap are never used nor removed. I then removed the hashmap from class X, rerun some data and the amount of memory used by the program dropped by about 25%.


Is there a tool which will analyze java code and help me find problem spots like this easier?

ack
Mar 16, 2007
ack ack ack ack

WalletBeef posted:

A bit of backstory before I get to my question ...
[...]
Is there a tool which will analyze java code and help me find problem spots like this easier?

Findbugs might be of some help, specially if you have loads of code and you're not familiar with it. Beware of false positives, though.

HFX
Nov 29, 2004

WalletBeef posted:

A bit of backstory before I get to my question ...

I have recently been tasked with making a java application "better". This application doesn't seem to have any cpu bottlenecks but it uses LOTS of memory.

So I fire up the latest version of JProfiler and discover that Class X has MANY instances that take up about LOTS of memory. So I take a look at the source and Class X has a bunch of data members, one of which is a hashmap. The only reference to this hashmap is a .put call. The contents of the hashmap are never used nor removed. I then removed the hashmap from class X, rerun some data and the amount of memory used by the program dropped by about 25%.


Is there a tool which will analyze java code and help me find problem spots like this easier?

You are doing one of the best things available. Also, try to watch how many generations things are living. Honestly, if I've learned one thing, people in general right terrible code and you will spend hours fixing there gently caress ups.

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost
You're correct to look at jProfiler first to address basic performance problems of any sort. The only stuff beyond that would be things similar to Rational's Purify software, which is mostly of use to C/C++ developers and which could arguably be made mostly useless via use of Electric Fence. Code analyzers can be a bit misleading if you try to turn them into metrics generators or something. The best analyzers to me spit out run-time statistics of objects and suggest items that could be turned static for better refactoring (there was a custom in-house tool I had before to do this, there's probably a commercial one around).

One thing to watch out for when messing with the fun that is garbage collection is to actually understand what the metrics of garbage collection mean and to avoid tweaking. Holly Cummins from IBM had a pretty comprehensive overview of garbage collection at a talk I watched online a while ago, and at the very least you should remember some quick points that will help you avoid a lot of common pitfalls.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

HFX posted:

people in general write terrible code and you will spend hours fixing their gently caress ups.
They confuse homophones too.

HFX
Nov 29, 2004

Jethro posted:

They confuse homophones too.

This is the code thread not the grammar thread.
The above example is why you don't write code while swapping back and forth between screens.

sonic bed head
Dec 18, 2003

this is naturual, baby!
Is there a way that I can get a simple date format or something else to print out something like "4th July 2007"? I can't find something that prints out days with "th" or "st" next to them but I feel like I'm missing something stupid. Thanks.

edb
Sep 28, 2008

Make me a sandwich.

sonic bed head posted:

Is there a way that I can get a simple date format or something else to print out something like "4th July 2007"? I can't find something that prints out days with "th" or "st" next to them but I feel like I'm missing something stupid. Thanks.

Well every date has 'th' after it apart from 1,2,3,21,22 and 23 so you could just write the method yourself.

EDIT: And of course the 31st.

edb fucked around with this message at 18:00 on Jun 5, 2009

Volguus
Mar 3, 2009

sonic bed head posted:

Is there a way that I can get a simple date format or something else to print out something like "4th July 2007"? I can't find something that prints out days with "th" or "st" next to them but I feel like I'm missing something stupid. Thanks.

Never had that request from anyone, usually July 4,2007 is just fine (or Jul 04,2007). And that, as you probably already know, can be done with DateFormat (or SimpleDateFormat if you have a non-standard pattern).

And, surprisingly, not even creating your own DateFormatSymbols class will help you.

It looks like you have to create your own DateFormat implementation. (that will have to accept only English locales.... :))

hey mom its 420
May 12, 2007

I'm using SAX2 to get some XML over a socket and parse it.
I first make a socket and then pass the InputStream from that socket and my DefaultHandler to the parse method.
What I'd like to do is to finish parsing once the root XML element has been closed and leave the socket open so I can write something back to it. What's the best way to do this?
Basically the parse method never yields control. If I force it to yield control by throwing an exception from, say, my overriden endElement method in my handler, it closes the socket connection, which I don't want it to do.

HFX
Nov 29, 2004

Bonus posted:

I'm using SAX2 to get some XML over a socket and parse it.
I first make a socket and then pass the InputStream from that socket and my DefaultHandler to the parse method.
What I'd like to do is to finish parsing once the root XML element has been closed and leave the socket open so I can write something back to it. What's the best way to do this?
Basically the parse method never yields control. If I force it to yield control by throwing an exception from, say, my overriden endElement method in my handler, it closes the socket connection, which I don't want it to do.

You have two approaches here. The parser only has control of the input stream of the socket. You still can have control of the output stream. You thus can write this as a multithreaded solution or single threaded solution.

Multithreaded would be on the endElement, signal the second thread to wake up and start writing to the multithreaded (it default sleeps, unless signaled).

Single threaded solution is to have your even handler hold a reference to the output stream. When you get your end element, you use the output stream and beginning writing whatever you want to the output stream before closing and exiting.

Max Facetime
Apr 18, 2009

You could create your own InputStream class that wraps the underlying socket stream, then just ignore the close-method call that the parser makes. This is actually not as bad of a hack as it sounds; you would be treating a part of the underlying stream as a stand-alone stream.

Clanpot Shake
Aug 10, 2006
shake shake!

I have a quick question regarding Eclipse. I'm trying to connect multiple packages I've written myself.

I've got a folder called src. In src are 3 folders that contain the source code for 3 different packages.

The package explorer in Eclipse looks something like this (+ being the dropdown arrow thing):
Project
+src
++pack1
+++file.java
++pack2
+++otherfile.java
++pack3
+++otherotherfile.java

How do I set up Eclipse to have the 3 packages visible to each other?

edit: Helpful goon helped me out via IM. ctrl+shift+o is super useful. Thanks Shavnir!

Clanpot Shake fucked around with this message at 01:21 on Jun 8, 2009

Shavnir
Apr 5, 2005

A MAN'S DREAM CAN NEVER DIE

Clanpot Shake posted:

I have a quick question regarding Eclipse. I'm trying to connect multiple packages I've written myself.

I've got a folder called src. In src are 3 folders that contain the source code for 3 different packages.

The package explorer in Eclipse looks something like this (+ being the dropdown arrow thing):
Project
+src
++pack1
+++file.java
++pack2
+++otherfile.java
++pack3
+++otherotherfile.java

How do I set up Eclipse to have the 3 packages visible to each other?

What do you mean visible to each other? Do you want to see them listed next to each other or something?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Shavnir posted:

What do you mean visible to each other? Do you want to see them listed next to each other or something?

I think he wants them to be automatically imported when he creates a new class.

Shavnir
Apr 5, 2005

A MAN'S DREAM CAN NEVER DIE

MEAT TREAT posted:

I think he wants them to be automatically imported when he creates a new class.

I talked to him a bit and helped him figure it out, it was just a bit of confusion as to how fully quantified class names are represented in import statements.

krooj
Dec 2, 2006
Question - JavaFX is piqued my interest as a cleaner alternative to the poo poo-show that is AJAX, but I am wondering what the difference is between JavaFX and a regular ol' Applet. Any huge diff?

1337JiveTurkey
Feb 17, 2005

krooj posted:

Question - JavaFX is piqued my interest as a cleaner alternative to the poo poo-show that is AJAX, but I am wondering what the difference is between JavaFX and a regular ol' Applet. Any huge diff?

It's got a more declarative language for making UIs and it's supposed to be multiplatform as well. As long as it doesn't depend on any libraries that wouldn't be available on a mobile platform, it'll work on that as well. It also supports WebStart so that it can be used as a standalone application by putting a JNLP file on the server.

almostkorean
Jul 9, 2001
eeeeeeeee
Can someone help me understand java classpath and packages? For some reason I just can't understand how it all works.

I'm doing research this summer and am trying to run this other guy's program. I edited my classpath so that I could compile his program, but when I run it I get:

code:
Exception in thread "main" java.lang.NoClassDefFoundError: BallTracker
Caused by: java.lang.ClassNotFoundException: BallTracker
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
I don't understand what the problem is because the BallTracker class definitely has a main function. The commands I'm using are:

code:
bash-3.2$ javac *.java
bash-3.2$ java BallTracker
and I'm in the directory that BallTracker.java is in. Can someone help me or point me to good tutorial or something?

Volguus
Mar 3, 2009
well, the official Java tutorial may be a good start: http://java.sun.com/docs/books/tutorial/index.html

Basically, it should work like this (command prompt):

javac HelloWorld.java

ls (or dir):
HelloWorld.java
HelloWorld.class

"java HelloWorld" should just work.

edit:
You may find it easier to use an IDE however (even if its not required). NetBeans and Eclipse are 2 of the most popular out there (and free).

Volguus fucked around with this message at 19:15 on Jun 8, 2009

hey mom its 420
May 12, 2007

Thanks, I passed the output stream to the event handler for the last element and replied from there.

placid
Jul 11, 2006

almostkorean posted:

I don't understand what the problem is because the BallTracker class definitely has a main function. The commands I'm using are:

code:
bash-3.2$ javac *.java
bash-3.2$ java BallTracker
and I'm in the directory that BallTracker.java is in. Can someone help me or point me to good tutorial or something?

What did you set the classpath to? Just because you're in the directory that BallTracker.java is in doesn't mean it's part of your classpath, you'd still need a period to represent that directory. An easy way to test this:

code:
java -cp $CLASSPATH;. BallTracker

8ender
Sep 24, 2003

clown is watching you sleep

1337JiveTurkey posted:

It's got a more declarative language for making UIs and it's supposed to be multiplatform as well. As long as it doesn't depend on any libraries that wouldn't be available on a mobile platform, it'll work on that as well. It also supports WebStart so that it can be used as a standalone application by putting a JNLP file on the server.

It looks cool but does anyone else feel like we've come full circle here? It really does feel like an old fashioned applet.

Volguus
Mar 3, 2009

8ender posted:

It looks cool but does anyone else feel like we've come full circle here? It really does feel like an old fashioned applet.

There is quite a big difference between an applet and an application loaded via WebStart (JavaFX or not).

The applet runs within the browser, specified as an <object> tag. It is obviously subject to a very restrictive set of permissions. Before JDK 1.6 update 10, the applet would run in the same process as the browser, and all applets in a web page would run in the same JVM. After JDK 1.6 update 10 (Oct 2008) some changes occurred:
- Applets run in a separate process to the browser, and each applet can optionally run in a separate JVM instance
- Applets can have parameters passed to them to control behaviour, such as setting a large initial heap size to avoid out of memory exceptions
- Applets can now be dragged out of the web browser and run as separate applications

(this info I copied from http://big.faceless.org/blog/2009/01/05/1231151940000.html Credit goes to them :) )

Oh, and applets could be somewhat interacted with via javascript (and viceversa via a netscape library).

JNLP was always a bit different. Via WebStart the user would run a stand-alone desktop application. It would be sandboxed of course (can get out via signature), but you would not have an JApplet class, you needed to have a class with a "main" method,etc. It always ran in a separate process, and was not accessible via javascript.

But yes, lately the line between an applet and a WebStart launched application has been blurred quite a bit.

zootm
Aug 8, 2006

We used to be better friends.
Other than the launching mechanism, one of the other big changes they made for Update 10 was significantly reducing the VM's startup time for applets. It's still slower than flash but it's not terribly, terribly slow now.

almostkorean
Jul 9, 2001
eeeeeeeee

placid posted:

What did you set the classpath to? Just because you're in the directory that BallTracker.java is in doesn't mean it's part of your classpath, you'd still need a period to represent that directory. An easy way to test this:

code:
java -cp $CLASSPATH;. BallTracker

That command doesn't work for me, but I still don't think I understand how it works. When I run
code:
 java BallTracker 
What exactly happens? Does it look through my classpath to find the right class file? And since I'm not compiling it in my classpath I'm getting this error?

Also, if I do:
code:
 java -cp . BallTracker
Shouldn't that tell it to look in the current directory for the class file? I tried this command and I get the same error

Merrack
Sep 15, 2007
Is BallTracker in a package?

almostkorean
Jul 9, 2001
eeeeeeeee

Merrack posted:

Is BallTracker in a package?

Yes, but I'm getting the same issue if I'm trying to run something that's not in a package

Edit: nevermind, java -cp . Blah works on something that's not in a package, how do I run something that's in a package?

ynef
Jun 12, 2002

almostkorean posted:

Yes, but I'm getting the same issue if I'm trying to run something that's not in a package

Edit: nevermind, java -cp . Blah works on something that's not in a package, how do I run something that's in a package?

You have to be at the top level directory, and then specify the Java class you want to run with the whole package information preserved. An example:

Say you have the following directory structure for your project:

code:
src/
src/com/somethingawful/forums/SomeCode.java
src/com/somethingawful/forums/SomeCode.class
lib/
res/
Now, src holds the source code. The SomeCode class is defined as being in the package "com.somethingawful.forums" (hence the directory structure). To run, you'd go to the "src/" folder and write "java -cp . com.somethingawful.forums.SomeCode"

almostkorean
Jul 9, 2001
eeeeeeeee

ynef posted:

You have to be at the top level directory, and then specify the Java class you want to run with the whole package information preserved. An example:

Say you have the following directory structure for your project:

code:
src/
src/com/somethingawful/forums/SomeCode.java
src/com/somethingawful/forums/SomeCode.class
lib/
res/
Now, src holds the source code. The SomeCode class is defined as being in the package "com.somethingawful.forums" (hence the directory structure). To run, you'd go to the "src/" folder and write "java -cp . com.somethingawful.forums.SomeCode"

YOU DA MAN. poo poo works now, thank you and other people above that helped

ssergE
Sep 10, 2001

No longer a stupid baby. Now a stupid teenager.
Hi All

I am starting to make the leap from standard JRE+odd libraries to investigate what is in the Java ecosphere of technologies.

Is there a good resource as to how it all fits together? It is quite daunting with Sprint, Struts, Hibernate, Jboss, Tomcat, JSP Faces, EJB, Seam, etc (please fill in the gaps).

It seems that there is just such an exhausting amount of cool stuff, and I am feeling a tad overwhelmed. Especially since there appears to be a lot of overlap between projects.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
There's definitely a huge array of software in the Java ecosystem, so you end up with too much choice and it's hard to tell what libraries are best to use.

What are you trying to accomplish? All those technologies have a place (it seems by the names you tossed out you're going to make a webapp) but some (like Jboss and EJB) are overkill for many projects, just like others (Faces...) are not robust enough for enterprise-grade systems.

ssergE
Sep 10, 2001

No longer a stupid baby. Now a stupid teenager.
Not necessarily a webapp, but mainly server-side apps that use things like queues, topics, persistence, etc.

Would I be correct in thinking that I need to concentrate on either Seam+JBossAS or Spring?

sonic bed head
Dec 18, 2003

this is naturual, baby!
Is it possible to programatically compile a JSP? I am using struts and I would like to dynamically make a jsp and then inject into it a request and get the value of the whole page as a huge string. Hopefully that makes sense. Basically I would like to be able to compile a jsp in the action without returning a jsp to the browser yet.

Volguus
Mar 3, 2009

sonic bed head posted:

Is it possible to programatically compile a JSP? I am using struts and I would like to dynamically make a jsp and then inject into it a request and get the value of the whole page as a huge string. Hopefully that makes sense. Basically I would like to be able to compile a jsp in the action without returning a jsp to the browser yet.

I am sure it's possible (no idea how, probably digging up the class that parses the JSP file in the tomcat distribution, and invoking that), but...why not create a servlet dynamically (JSP becomes a servlet anyhow, that gets compiled on the fly by tomcat)?
For that you'd only need to see the tools.jar for the javac compiler and invoke that.

That is, if you absolutely have to dynamically generate that. Since the details of what you have to do are zero, I can't say anything, but...are you sure there isn't a better solution?

sonic bed head
Dec 18, 2003

this is naturual, baby!
What I need to do is fairly complicated and I've been trying to piece it all together for weeks so let me try and explain.

I need to basically have dozens of forms each of which have complicated objects that aren't simple key value pairs. I need dozens of JSP's that correspond to each of these forms with completely different UI widgets (some are auto complete text boxes, some are multiselect boxes or textareas). Instead of actually making these dozens of jsp's that will be hell to maintain, I'm making a database driven UI. That would be fine if these JSP's were simple and I could use regular HTML, but the problem is that I need to use some struts tags to make this work correctly. The reason I need struts tags is that these forms don't really follow the regular http key/value pair idea, instead it is a more complicated object with nesting. Something like:
code:
{"id":123,
"object":{
"objparam1":"objvalue1",
"objparam2":"objvalue2"
}
}
Any idea how to better achieve what I'm trying to do?

edit:currently my very very clunky solution is to have html text input fields with names such as "123||||objparam1" and then split the name with "||||" and sort it in the backend, but that's getting more annoying to do when the nesting becomes deeper and I have things like "123||||category||||objgroup||||objparam1".

sonic bed head fucked around with this message at 15:48 on Jun 15, 2009

Volguus
Mar 3, 2009

sonic bed head posted:

What I need to do is fairly complicated and I've been trying to piece it all together for weeks so let me try and explain.

I need to basically have dozens of forms each of which have complicated objects that aren't simple key value pairs. I need dozens of JSP's that correspond to each of these forms with completely different UI widgets (some are auto complete text boxes, some are multiselect boxes or textareas). Instead of actually making these dozens of jsp's that will be hell to maintain, I'm making a database driven UI. That would be fine if these JSP's were simple and I could use regular HTML, but the problem is that I need to use some struts tags to make this work correctly. The reason I need struts tags is that these forms don't really follow the regular http key/value pair idea, instead it is a more complicated object with nesting. Something like:
code:
{"id":123,
"object":{
"objparam1":"objvalue1",
"objparam2":"objvalue2"
}
}
Any idea how to better achieve what I'm trying to do?

edit:currently my very very clunky solution is to have html text input fields with names such as "123||||objparam1" and then split the name with "||||" and sort it in the backend, but that's getting more annoying to do when the nesting becomes deeper and I have things like "123||||category||||objgroup||||objparam1".


Ideally, you could have one JSP generate the form on the fly, depending on what form is requested. What fields, objects,etc. are needed in the form, that can be kept in the database. There will need to be a factory created, that would be able to tell the JSP in a form-independent manner how to create the form, and be able to save the data.
The problem is that something like this would become quite complex (as such hard to maintain). An established design pattern would certainly help reduce the complexity, but the entire mechanism will resemble more a framework than an application.

JSP generation can work as well, but drat, I find this extremely complex to maintain as well (just think about it. A bug comes up. Where's the bug? In the JSP? In the generator? In the settings?). I've done this (servlet generation) a long time ago (2001-2002) and it was painful.

The simplest approach (more mindless work though) would be to just create the dozens or so JSP's that are needed. A student could be assigned to maintain them :)

Adbot
ADBOT LOVES YOU

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
I had to do something similar... what I ended up doing was this:
code:
<%
    StringWriter writer = new StringWriter();
    pageContext.pushBody(writer);
    pageContext.include("generator.jsp");
%>
generator.jsp was the page that I wanted the string content of. You have to wrap it in an include like that or (at least in Tomcat 6.0.20) it will not feed the StringWriter.

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