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
zootm
Aug 8, 2006

We used to be better friends.

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
Sorry I don't want to seem like I'm digging in at you, I'm just interested!

Adbot
ADBOT LOVES YOU

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

zootm posted:

Sorry I don't want to seem like I'm digging in at you, I'm just interested!

Heh, it's cool. Jaskell is open source, you can always take a look ;)

I've honsetly spent most of my "programmer education time" following Groovy, since it interfaces so well with Java. And learning Haskell as well as I can, but that's a bit off-topic.

mister_gosh
May 24, 2002

I really need to decompile some of my old code that I stupidly did not back up. I did not obfuscate (I think that is the word), to my knowledge. I compiled in Netbeans 5.x and it was compiled using java 1.4.x

I'm trying DJ and it is ok, but am wondering if there is anything better (commercial options are fine too).

Thanks in advance!!

epswing
Nov 4, 2003

Soiled Meat
I've had decent results using Cavaj.

MaxxBot
Oct 6, 2003

you could have clapped

you should have clapped!!
If I'm reading something from an InputStream using BufferedReader is there a way to make it only wait for a response a set amount of time?

If not is there any other way to read from an InputStream where I could do this?

I have a kind of a simple client server type thing with socket and serversocket going on and I'm at a point where the server asks all of the client a question at the same time. After this it analyzes their responses and if any of the clients fails to respond to the message then the server hangs up and thus screws everything up for all of the other clients. The only way to solve this that I know of is to make a new thread for every message I send out, but there has to be a better way than that.

EDIT: Nevermind, found the ready() method in bufferedreader

MaxxBot fucked around with this message at 01:26 on May 2, 2008

Fehler
Dec 14, 2004

.
How do I save an XML document I created with org.w3c.dom.Document to a file?

CrusaderSean
Jun 10, 2002
AwakenedDreamer
I'm reading up on design patterns and I saw this interface declaration:
public interface DrawingView extends ImageObserver, DrawingChangeListener {...stuff...}
I thought java doesn't support multiple inheritance?... By the way, DrawingChangeListener is another interface. so interface extends another_interface doesn't really count as inheritance or what?

csammis
Aug 26, 2003

Mental Institution

CrusaderSean posted:

I'm reading up on design patterns and I saw this interface declaration:
public interface DrawingView extends ImageObserver, DrawingChangeListener {...stuff...}
I thought java doesn't support multiple inheritance?...

It doesn't for classes, but an interface can extend (and a class can implement) multiple base interfaces.

CrusaderSean
Jun 10, 2002
AwakenedDreamer

csammis posted:

It doesn't for classes, but an interface can extend (and a class can implement) multiple base interfaces.

Ah, k. That makes sense.

zootm
Aug 8, 2006

We used to be better friends.

Fehler posted:

How do I save an XML document I created with org.w3c.dom.Document to a file?

Flying somewhat blind:

code:
Source source = new DOMSource( doc );
Result result = new StreamResult( new FileOutputStream( filename ) );

Transformer transformer = TransformerFactory.newInstance().newTransformer();

transformer.transform( source, result );
That might well work. I have not tried it, however. The default "transform" is identity if you're wondering what the deal is.

JingleBells
Jan 7, 2007

Oh what fun it is to see the Harriers win away!

zootm posted:

Flying somewhat blind:

code:
Source source = new DOMSource( doc );
Result result = new StreamResult( new FileOutputStream( filename ) );

Transformer transformer = TransformerFactory.newInstance().newTransformer();

transformer.transform( source, result );
That might well work. I have not tried it, however. The default "transform" is identity if you're wondering what the deal is.


The JAXP tutorial has the following example: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPXSLT4.html

zootm
Aug 8, 2006

We used to be better friends.

JingleBells posted:

The JAXP tutorial has the following example: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPXSLT4.html
So essentially that code example is right :). Excellent. I knew all that time fiddling with XML in Java was not in vain.

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

Fehler
Dec 14, 2004

.
Looks like it works, thanks! I guess something like doc.save(path) would have been too easy...

zootm
Aug 8, 2006

We used to be better friends.

Fehler posted:

Looks like it works, thanks! I guess something like doc.save(path) would have been too easy...
The DOM does not supply a way to save files, so it's not in the API. That sort of stuff is always a bit weird.

Manner Please
Dec 21, 2005

I am working on my AI homework (which I knew I shouldn't be doing in Java), but nonetheless, I ran into a problem that has challenged my understanding of object passing in Java. This question is in the context of the Eclipse IDE.

I have a State class. I have an ArrayList named stateSpace that contains all of the states in the problem. I am now thinking I should change this to a map, but whatever. My problem is that each of these States also contain their own ArrayList of states named transitions which contain the 8 possible states they could transition to based on an action.

Here's the problem:

When I go about setting these transitions, I search my stateSpace array for an appropriate state and add it to the given state's transition array. However,
in the Eclipse debugger, I'm looking at the same state in both the stateSpace list and a transitions list that contains it. They both have the same Eclipse debugger id number (which implies to me they are a pointer to the same object). However, the one in the stateSpace lists all of the appropriate fields it should have, while the one in transitions only has one and is missing a ton of information. What is going on?

Is there something fundamnetally wrong with adding a state to a list, then modifying that state in a different context and expecting it to be changed in the list?

poopiehead
Oct 6, 2004

It sounds like what you're doing is valid. Try highlighting the variable that isn't getting updated right and pressing ctrl+shift+I and see if it gives you a more up to date picture. You could also try showing the Display view and writing a line of code to access one of the members in question and then hit the run button or highlight + ctrl+shift+I.

Manner Please
Dec 21, 2005

poopiehead posted:

It sounds like what you're doing is valid. Try highlighting the variable that isn't getting updated right and pressing ctrl+shift+I and see if it gives you a more up to date picture. You could also try showing the Display view and writing a line of code to access one of the members in question and then hit the run button or highlight + ctrl+shift+I.

Thanks, I think you're right, Java wasn't the problem, Eclipse just arbitrary decided to give me less information then was there.

Gone Fission
Apr 7, 2007

We're here to make coffee metal. We're here to make everything metal.
I'm making a Java app, in NetBeans, that uses a library in a .jar file (JYaml if it's important). I want to package this library with the project, so that the project can be moved around and compiled and run on anywhere on any computer.

After quite a bit of searching and screwing around with NetBeans, it seems that NetBeans doesn't really have any way of referencing a relative path to a library.

I've managed to get it to work anyways by putting the .jar file in my /src directory and editing the project's project.properties file to include the line
code:
libs.JYaml.classpath=src/jyaml-1.3.jar
On opening the project, NetBeans alerts you to a Reference Problem. You don't have to fix it, and it still compiles and runs fine. It is pretty ugly though, and I will have to show the code to other people, so I'd like to get rid of it.

What I think happens is that it looks for the library in the absolute path, doesn't find it, shows the error message, and then my relative path in project.properties gets substituted in, the library is accessible now, but NetBeans doesn't get rid of the error message because it's "Resolve Reference Problem" button wasn't clicked.

Any way to fix this properly?

zootm
Aug 8, 2006

We used to be better friends.
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.

Gone Fission
Apr 7, 2007

We're here to make coffee metal. We're here to make everything metal.
That's the thing, NetBeans doesn't let me just type a path, or use any other way of setting a relative path. When I try to add a library classpath, I always get a "Select a folder" dialogue box, which always returns an absolute path.

Alan Greenspan
Jun 17, 2001

Is there an easy way to put breakpoints on Java library functions? Let's say I want to put a breakpoint on one of the constructors of java.io.File.

IDE is Eclipse but I'm willing to switch to any IDE for this.

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

Alan Greenspan posted:

Is there an easy way to put breakpoints on Java library functions? Let's say I want to put a breakpoint on one of the constructors of java.io.File.

IDE is Eclipse but I'm willing to switch to any IDE for this.

Yeah, in Eclipse your java source should already be linked to the rt.jar file. Just ctrl+click on a new File() statement and it'll take you right to the constructor. If that doesn't work, you probably have to link the source. I can tell you how to do that if you need.

Alan Greenspan
Jun 17, 2001

TRex EaterofCars posted:

Yeah, in Eclipse your java source should already be linked to the rt.jar file. Just ctrl+click on a new File() statement and it'll take you right to the constructor. If that doesn't work, you probably have to link the source. I can tell you how to do that if you need.

Thanks for the reply but apparently this is tricky because when I set a breakpoint into the rt.jar source ...



:saddowns:

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

Alan Greenspan posted:

Thanks for the reply but apparently this is tricky because when I set a breakpoint into the rt.jar source ...

:saddowns:

What versions of Eclipse and Java are you running?

Emo.fm
Jan 2, 2007
What's the deal with loading images from the web to display in applets? I'm following the directions from this site, but whenever I try to compile, I'm told that the method "getImage()" doesn't exist.

I'm doing this within a file that is a smaller class used in the Applet, not the Applet class itself, so could that be the problem? Pretty much the entire program is finished except for this (I was previously working offline as an application, and therefore just using a toolKit) so I'm not willing to move the loading of graphics into the Applet class, unless that's the only way. Ideas?

(Just for clarification, here is exactly how I'm trying to implement this:
code:
 Image myImage = getImage("http://www.website.com/image.gif"); 

Alan Greenspan
Jun 17, 2001

TRex EaterofCars posted:

What versions of Eclipse and Java are you running?

I figured it out. I was using the rt.jar from the real JRE, using the rt.jar from the JRE shipped with the JDK worked. Thank you. :woop:

ynef
Jun 12, 2002

Emo.fm posted:

What's the deal with loading images from the web to display in applets? I'm following the directions from this site, but whenever I try to compile, I'm told that the method "getImage()" doesn't exist.

I'm doing this within a file that is a smaller class used in the Applet, not the Applet class itself, so could that be the problem? Pretty much the entire program is finished except for this (I was previously working offline as an application, and therefore just using a toolKit) so I'm not willing to move the loading of graphics into the Applet class, unless that's the only way. Ideas?

(Just for clarification, here is exactly how I'm trying to implement this:
code:
 Image myImage = getImage("http://www.website.com/image.gif"); 

You answered your own question, there. Your code attempts to call a method called getImage() in your small class that is used in the Applet class. If you haven't written a method like that, then it is not going to work. Call the Applet class' getImage() method instead.

Emo.fm
Jan 2, 2007

ynef posted:

You answered your own question, there. Your code attempts to call a method called getImage() in your small class that is used in the Applet class. If you haven't written a method like that, then it is not going to work. Call the Applet class' getImage() method instead.

Sorry, I'm still a little confused. Let's say I have two files, MyApplet.java and SmallClass.java. If I want to load an image in SmallClass.java, how do I do it? Moving all of the image related operations that occur in SmallClass to MyApplet is not an option for me right now. There must be some way to load an image in a class that isn't an applet, right?

PS - If what you meant was to call Image myImage = MyApplet.getImage("url"); that doesn't seem to work either.

Incoherence
May 22, 2004

POYO AND TEAR

Emo.fm posted:

Sorry, I'm still a little confused. Let's say I have two files, MyApplet.java and SmallClass.java. If I want to load an image in SmallClass.java, how do I do it? Moving all of the image related operations that occur in SmallClass to MyApplet is not an option for me right now. There must be some way to load an image in a class that isn't an applet, right?
One possibility is to give SmallClass a reference to MyApplet, assuming that you're making SmallClass from MyApplet.

Thlom
Feb 24, 2008
Stupid question. I have literally tried everything too solve this, including rewriting the classes in question, but no luck.

I have a class with some methods which queries a database and returns the result set as an arraylist of strings. And in my gui-class I try to populate some comboboxes with the arraylists. Since swing apparently can't use arraylists I have tried to convert the arraylists to vectors with no luck, and arrays of strings, with no luck. I have even tried to get the methods to return Vectors. No matter what I do I get nullpointerexception-error.

database-class:
code:
 public class MySQL {
	private Connection con;
	private String url = "jdbc:mysql://serverinquestion.com:3306/";
	private String driver = "com.mysql.jdbc.Driver";
	private String db = "test";
	private String user = "usrname";
	private String pass= "passwd";

	public MySQL() {
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url + db, user, pass);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

        public ArrayList<String> getCategories() {
		try {
			ArrayList<String> clist = new ArrayList<String>();

			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT DISTINCT(kategori) FROM vare");

			while (rs.next()) {
				clist.add(rs.getString("kategori"));
			}

			return clist;

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
...
In the gui-class I have tried variations of this:
code:
private JComboBox comboCatBox;
private String[] listData = (String[]) mysql.getCategories().toArray(new String[mysql.getCategories().size()-1]);

...

public void createComponents() {
		comboCatBox = new JComboBox(listData);
...
}
After some investigation it seems like the nullpointerexceptions gets thrown at
code:
Statement stmt = con.createStatement();
Any help is appreciated. Tell me if I should offer more information.

epswing
Nov 4, 2003

Soiled Meat

Thlom posted:

After some investigation it seems like the nullpointerexceptions gets thrown at
code:
Statement stmt = con.createStatement();

Looks like you may have a connection problem. The rest of the implementation looks like it would work. Check if con == null, and if it is, then you were never connected in the first place, so that's what you should focus on at this point.

Thlom
Feb 24, 2008

epswing posted:

Looks like you may have a connection problem. The rest of the implementation looks like it would work. Check if con == null, and if it is, then you were never connected in the first place, so that's what you should focus on at this point.
Thanks for the hint, but shouldn't that return a SQLException? Anyway, I'll look into it. Thanks again.

epswing
Nov 4, 2003

Soiled Meat
Well the docs say "SQLException - if a database access error occurs". They don't say whether "access error" means there was an error with your login information (permission problem), or if there was an error communicating with the database.

In any case, a NPE occurring at a line with obj.methodCall() means obj was null.

Thlom
Feb 24, 2008

epswing posted:

Well the docs say "SQLException - if a database access error occurs". They don't say whether "access error" means there was an error with your login information (permission problem), or if there was an error communicating with the database.

In any case, a NPE occurring at a line with obj.methodCall() means obj was null.
Thanks for helping, you were right. Apparently I called the MySQL-class three lines too late, so no connection where open when I called the methods. When I moved it up everything worked. Can't believe I wasted several hours on this. Sorry for wasting your time, I will go and throw my self out of the window now.

1337JiveTurkey
Feb 17, 2005

Thlom posted:

Sorry for wasting your time, I will go and throw my self out of the window now.

Just make sure you haven't dispose()d your Graphics2D context first.

ynef
Jun 12, 2002

Emo.fm posted:

PS - If what you meant was to call Image myImage = MyApplet.getImage("url"); that doesn't seem to work either.

No, that doesn't work because getImage() is not a static method. Do you have a reference to an object of MyApplet? If so, you should be able to call the method that way. Either way, I think you should review your textbook again concerning references and method calls.

mister_gosh
May 24, 2002

I am running a third party command line based application through a Runtime call. This is using Java1.4. Essentially I have:

Process p = Runtime.getRuntime().exec("cmd.exe /c "+thirdPartyCommand);

The command takes about 2 seconds to run but in order to complete the command, it prompts you with a yes or no question. I always, programmatically, want to answer "y", but how do I pass that to the single Runtime command?

To clarify, currently I am running this third party application but want to serve the application to my users through a servlet. So I figured I would just use a Runtime system call, but I forgot about the yes/no prompt that needs to be answered in order for the application to finish.

Thanks in advance!!

1337JiveTurkey
Feb 17, 2005

mister_gosh posted:

I am running a third party command line based application through a Runtime call. This is using Java1.4. Essentially I have:

Process p = Runtime.getRuntime().exec("cmd.exe /c "+thirdPartyCommand);

The command takes about 2 seconds to run but in order to complete the command, it prompts you with a yes or no question. I always, programmatically, want to answer "y", but how do I pass that to the single Runtime command?

To clarify, currently I am running this third party application but want to serve the application to my users through a servlet. So I figured I would just use a Runtime system call, but I forgot about the yes/no prompt that needs to be answered in order for the application to finish.

Thanks in advance!!

Just call p.getOutputStream() (keeping in mind that the OutputStream is really the InputStream for the other process) and then wrap the Stream you get in a BufferedWriter or whatever and then send a 'y' through that. Then you can wait for it to finish with p.waitFor which returns the exit value of the terminated process.

mister_gosh
May 24, 2002

1337JiveTurkey posted:

Just call p.getOutputStream() (keeping in mind that the OutputStream is really the InputStream for the other process) and then wrap the Stream you get in a BufferedWriter or whatever and then send a 'y' through that. Then you can wait for it to finish with p.waitFor which returns the exit value of the terminated process.

Thanks! Works great :)

Adbot
ADBOT LOVES YOU

adante
Sep 18, 2003
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.

e.g.:

code:
public class test4 {

	public class A implements C { }
	public class B extends A {}
	public interface C {}
	
	public static void main (String [] args)
	{
		test4 t4 = new test4();
		B b = t4.new B();
		
		if (b instanceof A) System.out.println("b instanceof a");
		if (b instanceof C) System.out.println("b instanceof c");
		
		Class<?> bClass = b.getClass();
		
		if (bClass == B.class) System.out.println("bClass is B.class");
		
		// what do I do here to determine if bClass implements or extends A,C or even B or any arbitrary class?
	}
}
I couldn't seem to find a way to do this with a nice and clean single method call and this surprised me. I wrote a function which seems to do the job but as I'm new to reflection and do not really trust my code if there IS an api method it'd naturally be preferable.

code:
	public static boolean implementsClass(Class<?> type, Class<?> clasz)
	{
		do {
			if (type == clasz)
				return true;

			for (Class<?> ifc : type.getInterfaces())
				if (ifc == clasz)
					return true;

			type = type.getSuperclass();
		} while (type != null);
		
		return false;
	}

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