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
firetech
Oct 26, 2005

Fly posted:

edit: Probably nevermind what I wrote. Authkit seems to have little to do with Java security but instead with Mac OS X security, i.e., running with root privileges.

Affirmative. Nevertheless, I appreciate your help. What I'm trying to do is get access to the network interface which requires admin privs (at the level of the operating system, not within the Java framework). I don't want to require that people run my program with sudo or anything tricky.

Adbot
ADBOT LOVES YOU

ijustam
Jun 20, 2005

This is mostly a design question. I'm coding a gradebook program as a learning experience, and I've run into a snag.

Flow:
1. Course objects are loaded from the database. If the database doesn't exist, create it. These courses are loaded into an array list for easier handling. This is pretty basic and works fine.

2. If a course is added, it's appended to the array list. When the array list is pushed to the database, it needs to see this change and insert it. Problem: How do I compare beyond just comparing strings?

3. Courses need to have assignments, etc. Naturally this is done in a one-to-many relationship, linked by course ID. Problem: How do I match course ID to course object? For existing courses this isn't a huge deal, but for new courses that haven't been pushed to the database yet, this is a big deal (they don't have an ID!) Should I create a method that looks for the last available row and have it assume an ID? That seems a bit sketchy :(

zootm
Aug 8, 2006

We used to be better friends.

firetech posted:

Affirmative. Nevertheless, I appreciate your help. What I'm trying to do is get access to the network interface which requires admin privs (at the level of the operating system, not within the Java framework). I don't want to require that people run my program with sudo or anything tricky.
This is controlled on a per-process level so you need to actually spawn a new process (i.e. a new virtual machine) to do it. From the docs in the download:

quote:

In any Authorization implementation, some Privileges may only be effectively exercised by executing another program (process). The threshold at which this becomes necessary is implementation-dependent. On typical Unix-like systems, a process is the smallest entity that can have distinct enforceable privileges, rather than a thread, a class, or an object. Mac OS X works this way, so if special system-enforced privileges are needed, it may require a separate process to do it. That's why execPrivileged() exists.


ijustam posted:

Flow:
1. Course objects are loaded from the database. If the database doesn't exist, create it. These courses are loaded into an array list for easier handling. This is pretty basic and works fine.
It's usually best to use the capabilities of the database for things like this, rather than loading everything into memory.

ijustam posted:

2. If a course is added, it's appended to the array list. When the array list is pushed to the database, it needs to see this change and insert it. Problem: How do I compare beyond just comparing strings?

Usually it's sufficient to give them some artificial id (a number, say). Then you just update the row with the id you want to change. You go on to mention this in the next question.

ijustam posted:

3. Courses need to have assignments, etc. Naturally this is done in a one-to-many relationship, linked by course ID. Problem: How do I match course ID to course object? For existing courses this isn't a huge deal, but for new courses that haven't been pushed to the database yet, this is a big deal (they don't have an ID!) Should I create a method that looks for the last available row and have it assume an ID? That seems a bit sketchy :(
Most databases will do this for you in a "safe" way, using either auto-increment columns or sequences; what sort of database are you using? I get the feeling that you're inexperienced with databases in general - you seem to be assuming that you need to do more stuff in the Java code than you do.

zootm fucked around with this message at 13:21 on Jan 1, 2009

ijustam
Jun 20, 2005

zootm posted:

It's usually best to use the capabilities of the database for things like this, rather than loading everything into memory.

Usually it's sufficient to give them some artificial id (a number, say). Then you just update the row with the id you want to change. You go on to mention this in the next question.

Most databases will do this for you in a "safe" way, using either auto-increment columns or sequences; what sort of database are you using? I get the feeling that you're inexperienced with databases in general - you seem to be assuming that you need to do more stuff in the Java code than you do.

I'm using SQLiteJDBC. It's possible that there already exists something like it, but I haven't found it yet. There are a lot of column methods but very few row methods.

zootm
Aug 8, 2006

We used to be better friends.

ijustam posted:

I'm using SQLiteJDBC. It's possible that there already exists something like it, but I haven't found it yet. There are a lot of column methods but very few row methods.
For SQLite, look into ROWID for the row id generation you want.

With JDBC stuff you generally write your queries in SQL rather than calling Java methods to do it.

ijustam
Jun 20, 2005

zootm posted:

For SQLite, look into ROWID for the row id generation you want.

With JDBC stuff you generally write your queries in SQL rather than calling Java methods to do it.

OH yeah, that's just getRow(). Are you suggesting I do a "Select *" and just use the last row +1?

zootm
Aug 8, 2006

We used to be better friends.

ijustam posted:

OH yeah, that's just getRow(). Are you suggesting I do a "Select *" and just use the last row +1?
No. It gets set automatically. You need to do nothing to do it.

HFX
Nov 29, 2004

ijustam posted:

This is mostly a design question. I'm coding a gradebook program as a learning experience, and I've run into a snag.

2. If a course is added, it's appended to the array list. When the array list is pushed to the database, it needs to see this change and insert it. Problem: How do I compare beyond just comparing strings?

How are you pushing the array list to the database?

ijustam posted:

3. Courses need to have assignments, etc. Naturally this is done in a one-to-many relationship, linked by course ID. Problem: How do I match course ID to course object? For existing courses this isn't a huge deal, but for new courses that haven't been pushed to the database yet, this is a big deal (they don't have an ID!) Should I create a method that looks for the last available row and have it assume an ID? That seems a bit sketchy :(

Now I understand better what you are doing.

Here is how I would do it and will follow into your other questions.

Most good databases allow you to set a sequence which you ask for a number and it will only give you that number and no one else can ever get that number again (until it rolls over at least).
You write a insert query which queries the sequence for what it is inserting. You then ask the database for a query based on the course name to get that number. You will want to make course name unique obviously.

Now in memory you should store the course in a Hashmap or TreeMap. You'll probably use the sequence number for the key. If you make a change to this tree, you need a variable to modify it.

As for the assignments and such, you give them a field which takes the course id. Don't allow an assignment to be created which doesn't have a course id (can be handled through database triggers).

Never assume anything about the state of the database while in your Java code. Databases may change at any time and probably will if you have multiple users. So do not write any code assumes the database won't be modified while you are doing something in Java. Basically assume your program only has a snapshot at a particular time and things may have changed after it. Any code that relies on things being the same as the snapshot, needs to be changed to not make that assumption.

hexadecimal
Nov 23, 2008

by Fragmaster
I am making a webservice using Axis2 and using "application" scope.

I am wondering how I can make my webservice return data without XML? That is I want the response to HTTPRequests to my webservice to just contain whatever the function returned, and no stupid XML wrapping that information.

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost

Fehler posted:

Does anybody know where I could find sample code for comparing two WAV audio streams in Java? I need a function that returns some kind of "score" telling me how well the two streams match. From what I found on Google it looks like I need to do a Fourier Transformation, but I'm not really sure how to implement that and how to use it.
You're looking at not just FFTs but statistical methods to comparing data sets and signals like linear time series, clustering, Hidden Markov Models (HMMs), etc. Not exactly a walk in the park for beginners. I'm not aware of any Java libraries to do this though.

hexadecimal posted:

I am wondering how I can make my webservice return data without XML? That is I want the response to HTTPRequests to my webservice to just contain whatever the function returned, and no stupid XML wrapping that information.
So you're trying to turn it basically into a REST service because you're foregoing SOAP and XML-RPC. Axis2 theoretically supports REST-based messaging on HTTP transport (on the specs at least), but I haven't seen anyone manage to get it working.

hexadecimal
Nov 23, 2008

by Fragmaster

necrobobsledder posted:

You're looking at not just FFTs but statistical methods to comparing data sets and signals like linear time series, clustering, Hidden Markov Models (HMMs), etc. Not exactly a walk in the park for beginners. I'm not aware of any Java libraries to do this though.
So you're trying to turn it basically into a REST service because you're foregoing SOAP and XML-RPC. Axis2 theoretically supports REST-based messaging on HTTP transport (on the specs at least), but I haven't seen anyone manage to get it working.

EDIT: Never mind. I just ended up making a php script that strips out XML and responds with headers I need.

hexadecimal fucked around with this message at 18:09 on Jan 5, 2009

maskenfreiheit
Dec 30, 2004
...

maskenfreiheit fucked around with this message at 03:41 on Sep 29, 2010

csammis
Aug 26, 2003

Mental Institution
import statements have to go at the top of the file. I don't know how it got into the middle, but that's very wrong; the compiler even says exactly that (illegal start of expression).


e: What probably happened is the author intended each class to be their own files.
e2: Or maybe not because you defined GameHelper to be an inner class. Was that the author's code or your own?
e3: Hell, your braces don't line up to anything that lets me figure out what you or the author were going for here. Post complete code if there's a problem with it.

csammis fucked around with this message at 22:29 on Jan 6, 2009

lamentable dustman
Apr 13, 2007

🏆🏆🏆

GregNorc posted:

words....

They have a weird way of indenting but the GameHelper class should be it's own file (or keep it inside SimpleDotCom and move that import statement). You can only have an import at the head of a file. You'll have to fix the bracketing of the main class.

maskenfreiheit
Dec 30, 2004
Edit: Double Post

maskenfreiheit fucked around with this message at 21:31 on Mar 13, 2017

10011
Jul 22, 2007
If you have a public (non-inner) class called Something in package wherever.blah, it has to go into $CLASSPATH/wherever/blah/Something.java. The book probably should've mentioned this, but in future exercises when they have classes like this they mean them to go into separate files.

Also, you're missing two }s at the end of the first class.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

GregNorc posted:

Ok... yeah that's what I hate... the book is GREAT at making OO concepts understandable but it has the bad habit of putting a bunch of classes in one java file, then sometimes it wants you to keep a class by itself in it's own little file, and doesn't really distinguish between the two.

I'll try putting in it's own file. Thanks.

if you are still having problems this is how it should be. You had some other problems with spelling and stuff too
code:

public class SimpleDotCom {
	int[] locationCells;
	int numOfHits = 0;
	
	

	public void setLocationCells(int[] locs) {
		locationCells = locs;
	}

	public String checkYourself(String stringGuess) {
		int guess = Integer.parseInt(stringGuess);
		String result = "miss";
		for (int cell : locationCells) {
			if (guess == cell) {
				result = "hit";
				numOfHits++;
				break;
			}
		}
		if (numOfHits == locationCells.length) {
			result = "kill";
		}
		System.out.println(result);
		return result;
	}

	public static void main(String[] args) {
		int numOfGuesses = 0;

		SimpleDotCom theDotCom = new SimpleDotCom();
		GameHelper helper = new GameHelper();

		int randomNum = (int) (Math.random() * 5);

		int[] locations = { randomNum, randomNum + 1, randomNum + 2 };
		theDotCom.setLocationCells(locations);
		boolean isAlive = true;

		while (isAlive == true) {
			String guess = helper.getUserInput("enter a number");
			String result = theDotCom.checkYourself(guess);
			numOfGuesses++;
			if (result.equals("kill")) {
				isAlive = false;
				System.out.println("You took " + numOfGuesses + " guesses");
			}
		}
	}

}

code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GameHelper {
		public String getUserInput(String prompt) {
			String inputLine = null;
			System.out.print(prompt + " ");
			try {
				BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
				inputLine = is.readLine();
				if (inputLine.length() == 0)
					return null;
			} catch (IOException e) {
				System.out.println("IOException: " + e);
			}
			return inputLine;
		}
	}

hexadecimal
Nov 23, 2008

by Fragmaster
I think my webservice is running into dead locks, and makes the whole Axis2 freeze. Is there a way to configure Axis2 such that bugs in my webservice don't gently caress up the whole axis2 service?

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost

hexadecimal posted:

Is there a way to configure Axis2 such that bugs in my webservice don't gently caress up the whole axis2 service?
Wait, is it tying up the entire application server because Axis2 is hanging (an internal bug then), or can you run other axis2 based servlets in the application server?

hexadecimal
Nov 23, 2008

by Fragmaster

necrobobsledder posted:

Wait, is it tying up the entire application server because Axis2 is hanging (an internal bug then), or can you run other axis2 based servlets in the application server?

Other services stop working too. So I guess its tying up all of axis2. It can't even close by itself, I have to force quit it.

EDIT: Also I don't get any exceptions print out in TomCat window.

vvvv Sorry if this is a really newbie question, but where does tomcat store its logs?

hexadecimal fucked around with this message at 03:10 on Jan 7, 2009

lamentable dustman
Apr 13, 2007

🏆🏆🏆

what application server are you running?

anything in the logs?

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

hexadecimal posted:

vvvv Sorry if this is a really newbie question, but where does tomcat store its logs?
By default it's $CATALINA_HOME/logs/catalina.out where $CATALINA_HOME is the Tomcat installation directory.

hexadecimal
Nov 23, 2008

by Fragmaster

TRex EaterofCars posted:

By default it's $CATALINA_HOME/logs/catalina.out where $CATALINA_HOME is the Tomcat installation directory.

I see catalina.<date>.log no files with .out extension. I don't see any useful information in those log files, however.

I have a hunch it could be because it runs out of memory, however, and the reason is because I get java.lang.OutOfMemoryError: PermGen space in another java program I run (not webservice, but makes HTTPRequests to webservice) on the same machine.

Is this a valid hunch?

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Dunno but it easy to test. Increase java's default heap and max heap space. You can also increase the tomcat heap space in the config somewhere.

Where should also be 5 or 6 log files with different things in it.

hexadecimal
Nov 23, 2008

by Fragmaster

dvinnen posted:

Dunno but it easy to test. Increase java's default heap and max heap space. You can also increase the tomcat heap space in the config somewhere.

Where should also be 5 or 6 log files with different things in it.

I increased for tomcat, and it seems to work. At least it hasn't frozen in a bit. However it definitely doesn't work for the other app, but that is a separate issue.

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

hexadecimal posted:

I see catalina.<date>.log no files with .out extension. I don't see any useful information in those log files, however.

I have a hunch it could be because it runs out of memory, however, and the reason is because I get java.lang.OutOfMemoryError: PermGen space in another java program I run (not webservice, but makes HTTPRequests to webservice) on the same machine.

Is this a valid hunch?

Win32 or Linux or ?

hexadecimal
Nov 23, 2008

by Fragmaster

TRex EaterofCars posted:

Win32 or Linux or ?

Windows XP SP3. JDK 1.6

Bushibytes
Sep 3, 2006
Porcorate Potamocherus
If increasing your heap size just delays your problem, you might want to go one step further and analyze your memory usage. You can run tomcat with:

-XX:+HeapDumpOnOutOfMemoryError

This will give you a nice dump file when you crash, so that you can then analyze with some tools. I used an eclipse plugin called Memory Analyzer (MAT) to spot where my problem was in my case.

Jconsole also comes with the jdk in the bin directory, and it's pretty handy if you want to see live how your application is doing memory-wise. Although in my case, my memory issues were in the eden/tenured/old gens, not perm.

Flamadiddle
May 9, 2004

GregNorc posted:

Battleships stuff

Is it weird that reading through that the first thing I thought of was that the code doesn't take into account that you can only hit a single space once successfully? Unless that's a later part of the tutorial. Sounds like a decent book though. I was thinking of using Battleships as a learning example myself.

zootm
Aug 8, 2006

We used to be better friends.

hexadecimal posted:

I have a hunch it could be because it runs out of memory, however, and the reason is because I get java.lang.OutOfMemoryError: PermGen space in another java program I run (not webservice, but makes HTTPRequests to webservice) on the same machine.
PermGen space is where class definitions reside; if you load too many classes, it dies and completely screws the JVM since the GC will start spinning trying to free space. This is very annoying, and if you're seeing that error it is almost definitely the cause of your problem.

There's a couple of reasons this can happen in practice:
  • Some web/DI frameworks generate tons of classes (notably sites with loads of JSPs, I'm told, I imagine that the dynamic language frameworks might do this too but I don't know)
  • You have loads of classes loaded for some other legitimate reason (I suppose with lots of webapps on one server it'd be possible)
  • Web apps can leak classes on redeployment due to being impossible to deallocate
The latter one is a tremendous cause of pain. The reason it's caused is that when you redeploy a web application, the system dumps the ClassLoader which loaded the classes for the old webapp. However if these classes or any instances of them are referred to by static stuff outwith the webapp, they are still reachable and cannot be garbage collected. This means that every time you redeploy your app fresh copies of all of the classes are loaded into the permanent generation. It is silly. There's a number of common libraries with this problem, however.

If you don't think that is what you're actually seeing, it's pretty safe to just increase the amount of PermGen space the JVM has (it's quite low by default since most apps don't require that much). The arguments are:
code:
-XX:MaxPermSize=128m <- Maximum size
-XX:PermSize=72m <- Initial size (default is probably ok unless it dies immediately)
If the PermGen issue is due to the class leakage issue I mentioned before, however, it should only occur when you hot-redeploy your application. In that case if you can't fix the root problem you can mitigate the problem by restarting the webserver (yes, really) after each deploy. In a cluster this is less of a big deal since you can cascade them but it still really sucks.

zootm fucked around with this message at 14:53 on Jan 7, 2009

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
edited out

Hammerite fucked around with this message at 19:39 on Jan 7, 2009

dancavallaro
Sep 10, 2006
My title sucks
Which part of that is a Java question?

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Oh, I'm sorry. I didn't register the fact that this thread is "Java" rather than "Javascript".

Rhaegar
Jul 16, 2006
I'm writing an application that loads classes from jars at runtime. I'm doing this using the URLClassLoader.addURL method. One problem I'm running into is that in some cases I have two versions of the jar I want to use at different points of the application (it's a backwards compatibility test essentially). So what I need to do is unload the first version of the jar then reload another version of the jar so that when I use a class defined in that jar I can be sure that the class is of the correct version.

URLClassLoader does not have a way to unload jars or remove then from the URL because, I'm assuming, it's not that easy. I found one site that mentions that the custom class loaders in Tomcat (WebappClassLoader I assume) can do this but I've had a quick look at the code and I'm not seeing how it does it.

I had a couple of thoughts...

Can I set the URLClassLoader class to null and then call the JVM's garbage collector? Am I unloading all the classes that were loaded from jars in the URLClassLoader's URLClassPath?

Can I extend URLClassLoader to remove jars from the URLClassLoader's URLClassPath?

If anyone could give me any pointers that would be great. Class loading is something I've not really touched before in my Java programming so apologies if anything I've said above makes no sense. :) If there's a better way to achieve dynamic loading and unloading of different versions of the same class that would be even better!

Thanks.

Rhaegar fucked around with this message at 01:56 on Jan 8, 2009

hexadecimal
Nov 23, 2008

by Fragmaster
How do I get javadoc to generate documentation for inner private classes and their variables and methods? I am using javadoc tool from eclipse, but I could make my own .bat file for this if it's not possible to get eclipse to do this.

1337JiveTurkey
Feb 17, 2005

Rhaegar posted:

Can I set the URLClassLoader class to null and then call the JVM's garbage collector? Am I unloading all the classes that were loaded from jars in the URLClassLoader's URLClassPath?

Yep. Just make sure that you pay attention to the issues that were mentioned just a bit earlier about PermGen space. There can't be any dangling references to the ClassLoader, Classes or any instances for it to be collected so be careful with static references.

hexadecimal
Nov 23, 2008

by Fragmaster

zootm posted:

PermGen space is where class definitions reside; if you load too many classes, it dies and completely screws the JVM since the GC will start spinning trying to free space. This is very annoying, and if you're seeing that error it is almost definitely the cause of your problem.

There's a couple of reasons this can happen in practice:
  • Some web/DI frameworks generate tons of classes (notably sites with loads of JSPs, I'm told, I imagine that the dynamic language frameworks might do this too but I don't know)
  • You have loads of classes loaded for some other legitimate reason (I suppose with lots of webapps on one server it'd be possible)
  • Web apps can leak classes on redeployment due to being impossible to deallocate
The latter one is a tremendous cause of pain. The reason it's caused is that when you redeploy a web application, the system dumps the ClassLoader which loaded the classes for the old webapp. However if these classes or any instances of them are referred to by static stuff outwith the webapp, they are still reachable and cannot be garbage collected. This means that every time you redeploy your app fresh copies of all of the classes are loaded into the permanent generation. It is silly. There's a number of common libraries with this problem, however.

If you don't think that is what you're actually seeing, it's pretty safe to just increase the amount of PermGen space the JVM has (it's quite low by default since most apps don't require that much). The arguments are:
code:
-XX:MaxPermSize=128m <- Maximum size
-XX:PermSize=72m <- Initial size (default is probably ok unless it dies immediately)
If the PermGen issue is due to the class leakage issue I mentioned before, however, it should only occur when you hot-redeploy your application. In that case if you can't fix the root problem you can mitigate the problem by restarting the webserver (yes, really) after each deploy. In a cluster this is less of a big deal since you can cascade them but it still really sucks.

Thank you so much. This seems to have fixed the problem in my eclipse plugin.

However, I still have issue of axis2 locking up on me sometimes. I tried the option another user in this thread suggested, and no dumps or any useful logs are generated.

I have some bugs in my webservice, that are probably threading related. However when it becomes unresponsive it blocks all other webservices. How can I make sure that my buggy webservice doesn't make other webservices and axis2 cry, besides the obvious "fix the bugs in the program"?

hexadecimal
Nov 23, 2008

by Fragmaster
What does ../.. mean when its put in a class path?

dancavallaro
Sep 10, 2006
My title sucks

hexadecimal posted:

What does ../.. mean when its put in a class path?

Two directories above the current one?

Adbot
ADBOT LOVES YOU

The Bill
Sep 2, 2008

hexadecimal posted:

How do I get javadoc to generate documentation for inner private classes and their variables and methods? I am using javadoc tool from eclipse, but I could make my own .bat file for this if it's not possible to get eclipse to do this.

I think javadoc has a -private option (or something along those lines).

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