View Full Version : Can I use Java to...

12-03-2011, 04:47 AM
So I'm thinking about starting some Java project, but I want to know if I am able to do certain things before I even try.

So, with Java, can you:
- "dynamically" import packages
-- Suppose it isn't possible. Is it possible to then include a package that includes all packages within that package? Such that any new package that appears in that package is included, and any package removed won't be in there anymore.

- Have outer java class take priority over the classes in a jar
e.g. Suppose I had a jar file that had the file MyProjectStart.java. In the same folder as that jar file, if there is another MyProjectStart.java and you run the jar file, is it possible to have the outer java file run instead of the jar's java file?

- Minimize the project to "icon" bar [Windows]? Such that if the "X" or "Minimize" button are pressed, the program will be "hidden" from the task bar and is instead in the icon bar that is next tot he taskbar?

- Dynamically create classes using strings. Such that if you loaded a string "Foo" you can create a class Foo (and probably send in some arguments).

- Suppose some or all of these are possible. Is it possible to have these cross-OS compliant?

Also, what GUI package should I look in that works for most OS and most Java versions?

12-03-2011, 05:30 AM
You can dynamically import classes, but I don't think you can with a full package. You can of course load a class that does import many packages. The trick will be to give it a more meaningful datatype than that of Object. I'd recommend a simple interface which you can cast to.
This is done using either reflection or the ClassLoader. Here's a link: http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html
Priority in a jar, I'd say no. You have one chance to make an entrance class, and that cannot change. You can use a factory to select what to load based on some criteria though, which can be loaded from the filesystem.
You can run completely in the background by never discarding the gui. You can also make use of the task bar as well, let me find a link. Here we go: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/systemtray/
Simply override the close operation to hide it instead of closing it.
Yes, see the first point. That is how you dynamically load the class since there is no way prior to this to determine its datatype. Once a class has been established, you may cast it as such. Seems unusual yes, but that is how you load. Once a 'class' has been determined as valid, it may then be cast from an Object to another type.
Java is platform independent. Use the swing library with as minimal use of AWT as you can. AWT is more widespread version wise of java, while swing is lighter weight so its better for platform independent development.