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
Magicmat
Aug 14, 2000

I've got the worst fucking attorneys
I'm trying to figure out the best way to share an Eclipse project. The folder is on a shared drive (shared via Dropbox), with one computer on Mac OS X and the other using Windows 7.

When I try just pointing Eclipse to the folder as the workspace to open, I get bunch of "file not found" errors, and an empty project.

I'd rather not put the project into version control right now since it's just a small, personal project at the moment. Also, my VCS of choice (Git) kinda sucks on windows.

Adbot
ADBOT LOVES YOU

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Magicmat posted:

I'm trying to figure out the best way to share an Eclipse project. The folder is on a shared drive (shared via Dropbox), with one computer on Mac OS X and the other using Windows 7.

When I try just pointing Eclipse to the folder as the workspace to open, I get bunch of "file not found" errors, and an empty project.

I'd rather not put the project into version control right now since it's just a small, personal project at the moment. Also, my VCS of choice (Git) kinda sucks on windows.

Use mercurial with tortoisehg if you still want a decent DVCS on windows. There isn't any excuse.

Magicmat
Aug 14, 2000

I've got the worst fucking attorneys

MEAT TREAT posted:

Use mercurial with tortoisehg if you still want a decent DVCS on windows. There isn't any excuse.
I'm a Git man, though. Usually I do 90% of my dev work on OS X and when I do work on Windows, I keep it in the shared folder and do my commits from OS X. Failing that, I bite the bullet and use the Windows version of Git.

But that's getting off topic :) Is there an easy way to just open a Eclipse workspace on another system?

epswing
Nov 4, 2003

Soiled Meat
Just right click the project in the Project Explorer, export to zip, save the file into dropbox.

Then open eclipse on the other machine, right click empty space in Project Explorer (I think), import, select the zip file.

Magicmat posted:

I'm a Git man, though.

Oh quit moaning. For everyday use, the ideas (and many commands) are nearly the same. It's a chance to learn another DVCS. On your next job interview, you can say "yes, I know Mercurial basics." When you want to contribute to a project on BitBucket, you'll know what's going on. What have you got to lose, exactly?

Max Facetime
Apr 18, 2009

Maybe it would work using a local workspace on the Windows machine, then Import...->Existing Projects into Workspace, point it to \My Dropbox and then tell it to not copy the project files to the workspace?

I don't think putting the workspace itself to Dropbox is a good idea.

Chairman Steve
Mar 9, 2007
Whiter than sour cream
^^ Agreed.

Honestly, I'd like into using something that's IDE-agnostic, such as Maven, to build your project. It has an Eclipse plugin that can be used to generate Eclipse project artifacts by reading the project's POM.

Parantumaton
Jan 29, 2009


The OnLy ThInG
i LoVe MoRe
ThAn ChUgGiNg SeMeN
iS gEtTiNg PaId To Be A
sOcIaL MeDiA sHiLl
FoR mIcRoSoFt
AnD nOkIa

Chairman Steve posted:

Honestly, I'd like into using something that's IDE-agnostic, such as Maven, to build your project.

This sentence started with such great potential... (I don't mean that Maven wouldn't be IDE-agnostic, it's more like reality agnostic).

danishcake
Aug 15, 2004
A quick question from a C++ guy who is dipping his toes into Java. If I have two preallocated objects, is there a convenient way to do a deep copy of one into the other, avoiding allocating a new one? I really want something a bit like clone() but with an existing target.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

danishcake posted:

A quick question from a C++ guy who is dipping his toes into Java. If I have two preallocated objects, is there a convenient way to do a deep copy of one into the other, avoiding allocating a new one? I really want something a bit like clone() but with an existing target.

If you are not opposed to using an external class the Apache Commons BeanUtils class has the copyProperties method that does what you want. But that if you have non primitive fields inside, it will probably just copy the references and not do a deep copy like you would expect.

Chairman Steve
Mar 9, 2007
Whiter than sour cream

Parantumaton posted:

This sentence started with such great potential... (I don't mean that Maven wouldn't be IDE-agnostic, it's more like reality agnostic).

Eh? Care to elaborate?

Magicmat
Aug 14, 2000

I've got the worst fucking attorneys
I'm having trouble with reading a webpage via a URLConnection object using a proxy. I'm using a Proxy object with the url.openConnection(proxy) method.

My code is designed to test a bunch of proxies, so what it's supposed to do is just try to access the same web page over and over, with a new proxy each time. Each proxy has no guarantee of being functional in any way (hence why I'm testing them.)

The problem is that Java seems to do it's own thing in terms of my proxy settings. For example, I have it setup to connect with no proxy, proxy A, proxy B and proxy C. My webserver's logs, however, show it being accessed with no proxy, no proxy, proxy B and proxy B. Sometimes it's no proxy, proxy A, proxy A, proxy C; or some other random combination.

What's going on here? Why is Java holding over old proxy info when I'm setting it on a per-connection basic?

You can see my horrible, messy, prototype-quality code here.

Magicmat fucked around with this message at 22:10 on Aug 23, 2010

Chairman Steve
Mar 9, 2007
Whiter than sour cream
Is it in any way possible that it has something to do with Java's caching of DNS resolution[1]?

http://myhowto.org/java/42-understanding-host-name-resolution-and-dns-behavior-in-java/

Magicmat
Aug 14, 2000

I've got the worst fucking attorneys

Chairman Steve posted:

Is it in any way possible that it has something to do with Java's caching of DNS resolution[1]?

http://myhowto.org/java/42-understanding-host-name-resolution-and-dns-behavior-in-java/
Hmm, I tried adding
code:
System.setProperty("networkaddress.cache.ttl", "0");
System.setProperty("networkaddress.cache.negative.ttl", "0");
to the start of my Main, but it didn't seem to affect anything. I could use the JNI method they detail in that article to flush the domain cache, but using JNI is something I'd rather avoid, if possible.

If there's no way around this, I may just write my own proxy-capable basic HTTP class over a Socket. Kinda sucky, but I'm not sure what else to do.

Max Facetime
Apr 18, 2009

Magicmat posted:

You can see my horrible, messy, prototype-quality code here.

You're not closing the connections or streams properly, so they might be closed only when the program terminates. This could affect the order and timestamps of the log entries the webserver outputs.

Also, try adding a 10 second delay between testing different proxies to see whether the duplicate log entries come from the same or different proxies.

Magicmat
Aug 14, 2000

I've got the worst fucking attorneys

I am in posted:

You're not closing the connections or streams properly, so they might be closed only when the program terminates. This could affect the order and timestamps of the log entries the webserver outputs.
Ah ha, I think that was it. Adding a disconnect() call followed by a close() call (the other order didn't work) seems to have fixed it, at least for my sample size of four proxies.

Thanks for the help!

Tomed2000
Jun 24, 2002

I'm pretty familiar with object-oriented Python and I used to use do some object-oriented C++ years ago so the Java syntax looks somewhat familiar to me. What are some are good resources for learning Java if I'm already pretty familiar with object-oriented design?

I found this interesting site which I'm going through now but wouldn't mind picking up a book if there are any good suggestions.

omlette-a-gogo
May 26, 2010
A good habit to get into is to close all streams in a finally block:

code:
InputStream in = null;
try {
    in = getInputStreamFromSomewhere();
    // Do stuff with the input stream
} catch (Exception ex) {
    // Handle it, or wrap and rethrow
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException ex2) {
            // Log it, or eat it.
        }
    }
}
In fact I do this so often I wrote a helper class that I am always reusing:

code:
public class IoHelper
{
    public static void close(InputStream in)
    {
        if (in != null) {
            try {
                in.close(); 
            } catch (IOException ex) {
                // Eat it.
            }
        }
    }

    public static void close(OutputStream out);
    public static void close(Writer w);
    public static void close(Reader r);
    // etc. for other closable classes
}

omlette-a-gogo
May 26, 2010

fletcher posted:

How come something like this works:
code:
public static String[] test() {
	String[] stuff = { "stuff" };
	return stuff;
}
but this does not:
code:
public static String[] test() {
	return { "stuff" };
}
(not that I want to use it, just curious)

Because the Java compiler is interpreting {"stuff"}; as a static initializer in the first case, and a block of code in the second, which makes no lexical sense. You could replace the second case with return new String[]{"stuff"}; and it should work fine.

Chairman Steve
Mar 9, 2007
Whiter than sour cream

omlette-a-gogo posted:

In fact I do this so often I wrote a helper class that I am always reusing:

http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html#closeQuietly(java.io.InputStream)

Learn to love Apache commons! :eng101:

Or for some Java 6 goodiness: http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Closeables.html#closeQuietly(java.io.Closeable)

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost
I'd still prefer to get RAII / RAII-like conventions like C#'s using, although Closeables could be turned into syntactic sugar like for ( : ) loops, but given the history of the JSRB, I only expect :psyduck: after :psyduck: Always hoped to have something like a synchronized block become an arbitrary block reflecting the scope of an object's lifetime with the end of the block automatically invoking the finalize method. But... garbage collection and :effort:

Parantumaton
Jan 29, 2009


The OnLy ThInG
i LoVe MoRe
ThAn ChUgGiNg SeMeN
iS gEtTiNg PaId To Be A
sOcIaL MeDiA sHiLl
FoR mIcRoSoFt
AnD nOkIa
JDK7 will have automatic resource management.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
When's that coming out, again?

TURTLE SLUT
Dec 12, 2005

I'm trying to have a Vector array with four Vectors containing four different types of objects that are all subclasses to the same superclass, so that I could refer to the Vectors with index numbers. This is how I'm trying to do it:
code:
Vector<SuperClass> vectors[] = new Vector[] { new Vector<SubClassOne>(),
                                              new Vector<SubClassTwo>(),
                                              new Vector<SubClassThree>(),
                                              new Vector<SubClassFour>()  };
However, this gives me a type safety warning. I've tried several different ways of expressing this and reading Java Generics tutorials and such, but it just keeps giving some kind of type mismatch error/warning and confusing me.

I'm very new to this whole Java thing - actually to programming in general - so I could be missing something obvious or trying to do this in a completely retarded way, I just don't know! Is this even possible, or is the type safety warning harmless?

edit: the exact type safety warning is "Type safety: The expression of type Vector[] needs unchecked conversion to conform to Vector<SuperClass>[]" :confused:

TURTLE SLUT fucked around with this message at 15:44 on Aug 26, 2010

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

Mustach posted:

When's that coming out, again?

You could theoretically start using it now, although I'm not sure how much of the Java TCK it actually passes. I'm hoping the one good thing to come out of the Oracle buy-out is a more intent focus on getting this poo poo out the door.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

Cukel posted:

I'm trying to have a Vector array with four Vectors containing four different types of objects that are all subclasses to the same superclass, so that I could refer to the Vectors with index numbers. This is how I'm trying to do it:
code:
Vector<SuperClass> vectors[] = new Vector[] { new Vector<SubClassOne>(),
                                              new Vector<SubClassTwo>(),
                                              new Vector<SubClassThree>(),
                                              new Vector<SubClassFour>()  };
However, this gives me a type safety warning. I've tried several different ways of expressing this and reading Java Generics tutorials and such, but it just keeps giving some kind of type mismatch error/warning and confusing me.

I'm very new to this whole Java thing - actually to programming in general - so I could be missing something obvious or trying to do this in a completely retarded way, I just don't know! Is this even possible, or is the type safety warning harmless?

edit: the exact type safety warning is "Type safety: The expression of type Vector[] needs unchecked conversion to conform to Vector<SuperClass>[]" :confused:

You can't create generic arrays in Java. The warning isn't something to be terribly worried about, as long as you're sure you're putting correctly subclassed vectors into the array.

More specifically, you probably should do something like:

code:
Vector<? extends SuperClass> vectors[] = new Vector[4];
vectors[0] = new Vector<SubClassOne>();
...
The way you've done it, on the RHS, you're creating a Vector[], and you could put anything in there. You could be putting { new Vector<Dick>(), new Vector<Butt>()... } on the RHS, and it wouldn't complain. But when you go to use it, you'd get some kind of runtime error.

However, if you do this:

code:
Vector<SuperClass>[] vectors = new Vector[4];
vectors[0] = new Vector<NotSubClass>();
vectors[1] = new Vector<SubClassOne>();
It gives you a type mismatch error on both lines 2 and 3. Using <? extends SuperClass> will allow line 3, but throws a type mismatch error on line 2.

Kilson fucked around with this message at 16:26 on Aug 26, 2010

OddObserver
Apr 3, 2009

Parantumaton posted:

JDK7 will have automatic resource management.

Any chance for a link on that? Can't seem to find it on the Sun^wOracle website.

TURTLE SLUT
Dec 12, 2005

Kilson posted:

You can't create generic arrays in Java.
Why is it a generic array? Just because I'm using Vectors? Because this gives the exact same warning:
code:
Vector<SubClassOne> vectors[] = new Vector[] { new Vector<SubClassOne>(),
                                              new Vector<SubClassOne>(),
                                              new Vector<SubClassOne>(),
                                              new Vector<SubClassOne>()  };
And there's no generics involved as much as I understand. Oh well, if it's not possible, guess I'll just have to make four different very similar methods for handling each of the Vectors, which is just dumb. :(

lewi
Sep 3, 2006
King

Cukel posted:

Why is it a generic array? Just because I'm using Vectors? Because this gives the exact same warning:
code:
Vector<SubClassOne> vectors[] = new Vector[] { new Vector<SubClassOne>(),
                                              new Vector<SubClassOne>(),
                                              new Vector<SubClassOne>(),
                                              new Vector<SubClassOne>()  };
And there's no generics involved as much as I understand. Oh well, if it's not possible, guess I'll just have to make four different very similar methods for handling each of the Vectors, which is just dumb. :(

Can you not make a Vector<Vector<SuperClass>>?

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

Cukel posted:

Why is it a generic array? Just because I'm using Vectors? Because this gives the exact same warning:

See my longer response above. A generic array would be something like

code:
new Vector<Blah>[];
Since you can't do that, but you have specified on the LHS that the array is of type Vector<SuperClass>[], you get the unchecked conversion warning.

TURTLE SLUT
Dec 12, 2005

Okay, thanks a lot for the help! Vector<Vector<SuperClass>> seems to work too and is much more fun, so I think I'm going to go with that. At least I tried it and didn't get any errors or warnings.

Max Facetime
Apr 18, 2009

OddObserver posted:

Any chance for a link on that? Can't seem to find it on the Sun^wOracle website.

It's called Project Coin. The proposal looks like to add a list of suppressed exceptions to Throwable, which deals neatly with the case where closing a resource throws an exception while another exception has already been thrown.

omlette-a-gogo
May 26, 2010

Cukel posted:

I'm trying to have a Vector array with four Vectors containing four different types of objects that are all subclasses to the same superclass, so that I could refer to the Vectors with index numbers. This is how I'm trying to do it:
code:
Vector<SuperClass> vectors[] = new Vector[] { new Vector<SubClassOne>(),
                                              new Vector<SubClassTwo>(),
                                              new Vector<SubClassThree>(),
                                              new Vector<SubClassFour>()  };
However, this gives me a type safety warning. I've tried several different ways of expressing this and reading Java Generics tutorials and such, but it just keeps giving some kind of type mismatch error/warning and confusing me.

I'm very new to this whole Java thing - actually to programming in general - so I could be missing something obvious or trying to do this in a completely retarded way, I just don't know! Is this even possible, or is the type safety warning harmless?

edit: the exact type safety warning is "Type safety: The expression of type Vector[] needs unchecked conversion to conform to Vector<SuperClass>[]" :confused:

You are much better off redesigning this by implementing a new class containing the four vectors as member variables then putting in whatever methods you might need to manipulate or access them. Also consider using List unless there is a specific need for Vector.

code:
public class FourList
{
    private List<SubTypeOne> m_listOne;
    // etc.

    public FourList()
    {
        m_listOne = new ArrayList<SubTypeOne>();
        //etc.
    }

    public List<SubTypeOne> getListOne()
    {
        return m_listOne;
    }
    //etc.
}
This will remove any ambiguity about which lists contain which datatypes.

omlette-a-gogo
May 26, 2010

Cukel posted:

Okay, thanks a lot for the help! Vector<Vector<SuperClass>> seems to work too and is much more fun, so I think I'm going to go with that. At least I tried it and didn't get any errors or warnings.

Better is Vector<Vector<? extends SuperClass>>. But this datatype implies that whoever has a reference to it can do all sorts of stuff that you might not want them to do, like add/remove data from the vectors or add/remove entire vectors from the top-level vector. If there is any kind of structure or access control that needs to be enforced you should build it in to a new class and not rely on users to do the right thing.

Parantumaton
Jan 29, 2009


The OnLy ThInG
i LoVe MoRe
ThAn ChUgGiNg SeMeN
iS gEtTiNg PaId To Be A
sOcIaL MeDiA sHiLl
FoR mIcRoSoFt
AnD nOkIa

OddObserver posted:

Any chance for a link on that? Can't seem to find it on the Sun^wOracle website.

http://blogs.sun.com/darcy/entry/project_coin_updated_arm_spec
http://blogs.sun.com/darcy/entry/project_coin_arm_implementation
http://blogs.sun.com/darcy/entry/project_coin_try_out_try

...and a bunch of other stuff from that blog.

TURTLE SLUT
Dec 12, 2005

omlette-a-gogo posted:

Better is Vector<Vector<? extends SuperClass>>. But this datatype implies that whoever has a reference to it can do all sorts of stuff that you might not want them to do, like add/remove data from the vectors or add/remove entire vectors from the top-level vector. If there is any kind of structure or access control that needs to be enforced you should build it in to a new class and not rely on users to do the right thing.
I tried making a Vector<Vector<? extends SuperClass>>, but when trying to add SubClasses into the Vector<? extends SuperClass>, I got weird cryptic errors. Something about type not applicable. Vector<Vector<SuperClass>> seems to work fine. This is quite confusing!

edit: of course I did not run any of this yet so I dunno if I'll get more problems then. Just going by what Eclipse is telling me.

TURTLE SLUT fucked around with this message at 22:51 on Aug 26, 2010

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Cukel posted:

Vector<Vector<? extends SuperClass>>

This type effectively disallows both adding child vectors to it and adding elements to its child vectors. I don't know why it was suggested.

Vector<Vector<SuperClass>> only works if the inner vectors have type Vector<SuperClass>. The reason why is complicated.

Without question, you should be using a class with instance fields that have the appropriate vector types.

omlette-a-gogo
May 26, 2010

rjmccall posted:

This type effectively disallows both adding child vectors to it and adding elements to its child vectors. I don't know why it was suggested.

Vector<Vector<SuperClass>> only works if the inner vectors have type Vector<SuperClass>. The reason why is complicated.

Without question, you should be using a class with instance fields that have the appropriate vector types.

It does work, but it's a meaningless argument, there has got to be a better way to model the data. Compressed to save space, not interested in finding out how many lines of code I can post before getting banned.

code:
import java.util.Vector;

abstract class Base { public abstract int getNum(); }
class SubOne extends Base { public int getNum() { return 1; } }
class SubTwo extends Base { public int getNum() { return 2; } }
class SubThree extends Base { public int getNum() { return 3; } }
class SubFour extends Base { public int getNum() { return 4; } }

public class VectorTest
{
    public static void main(String[] args)
    {
        Vector<Vector<? extends Base>> vv = new Vector<Vector<? extends Base>>();

        Vector<SubOne> v1 = new Vector<SubOne>();
        v1.add(new SubOne());
        v1.add(new SubOne());
        vv.add(v1);

        Vector<SubTwo> v2 = new Vector<SubTwo>();
        v2.add(new SubTwo());
        v2.add(new SubTwo());
        vv.add(v2);

        Vector<SubThree> v3 = new Vector<SubThree>();
        v3.add(new SubThree());
        v3.add(new SubThree());
        vv.add(v3);

        Vector<SubFour> v4 = new Vector<SubFour>();
        v4.add(new SubFour());
        v4.add(new SubFour());
        vv.add(v4);

        for (Vector<? extends Base> v : vv) {
            for (Base b : v) {
                System.out.println(b.getNum());
            }
        }
    }
}

TURTLE SLUT
Dec 12, 2005

rjmccall posted:

This type effectively disallows both adding child vectors to it and adding elements to its child vectors. I don't know why it was suggested.

Vector<Vector<SuperClass>> only works if the inner vectors have type Vector<SuperClass>. The reason why is complicated.

Without question, you should be using a class with instance fields that have the appropriate vector types.
Doesn't using a class without a Vector array or Vector<Vector> of some kind defeat the original purpose? Which was to be able to refer to the different Vector<SubClass>-fields with an index number. Or am I misunderstanding what you're saying somehow.

Right now I have a separate class that contains the Vector<Vector<SuperClass>>, and it simplified the class a bit and seems to work as expected in some tests I did. I'm still really fuzzy on why this exact implementation works or if it's even supposed to work, but eh.

tef
May 30, 2004

-> some l-system crap ->

omlette-a-gogo posted:

A good habit to get into is to close all streams in a finally block:

You can also use helper methods to handle all of the boilerplate:

code:
public static void main(String args[]) {
		readLines(new File("foo.txt"), new SimpleLineReader() {
			public void read(String line) {
				System.out.println("EOF");
			}
		});
	}
Where readlines, and SimpleLineReader are in some class:

code:
	interface LineReader {
		void read(String line);
		void eof();
		void error(IOException e);
	}

	static abstract class SimpleLineReader implements LineReader {
		public abstract void read(String line);
		public void eof() {}
		public void error(IOException e) {
			throw new RuntimeException(e);
		};
	}
	static boolean readLines(File f, LineReader r) {
		BufferedReader input = null;
		try {
			input =  new BufferedReader(new FileReader(f));
			while (true) {
				String line = input.readLine();
				if (line != null) {
					r.read(line);
				} else {
					break;
				}
			}
			r.eof();
			return true;
		} catch (IOException e) {
			r.error(e);
			return false;
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException ex2) {
					// Log it, or eat it.
				}
			}
		}
	}
I can't be arsed to test it, but hopefully you can see what i'm aiming at.

Adbot
ADBOT LOVES YOU

Chairman Steve
Mar 9, 2007
Whiter than sour cream
Or use the file-read methods of FileUtils[1] or Files[2].

1. http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html#readLines(java.io.File)
2. http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Files.html#readLines(java.io.File, java.nio.charset.Charset)

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