S Chan

Ranch Hand
+ Follow
since Jul 25, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by S Chan

Hi all,

Java newbie here. I am going to ask a newbie question here so please pardon me.

My question is - "what is the REAL use of classpath?" I want to clarify that I know how to use the -classpath parameter, so my question is not about 'what is classpath', but more of 'when and why use classpath?'

If I have some classes or packages, I would want to put inside my project workspace or in the lib folder of my projects so that they can be included during compile. Am I right on this? This is what I think a make-everthing-as-simple-as-possible approach it is, as far as I can reason myself. Why would I want to store some classes in some alien paths and include them using the -classpath parameters? Isn't that making things more complicated?

I have experience in working in a team project, and we used SVN to share source code. I understand that when there is some external libraries, it is wise to not put them in the SVN, but put them in the local storage in our individual working machines and link them with classpath... However, I find that this is quite troublesome when I set up the workspace for the first time or when I switch working environment/machines. I remember after getting the source from the SVN, the first successful compile is painful and taking a long time when there is no one telling me what external libraries are missing, what classpaths are to be set... I wonder how you experienced programmers "magically" know libraries are required when working on open/shared projects out there...

Another thing about classpath that leaves me puzzled is that, it is surely not distribution-friendly. Let's say I have made an executable Jar and distribute this to other people. I think double-clicking the Jar is the most intuitive way of starting the Jar for most people. If my Jar requires specific classpaths, I don't think there is an easy way to explain to them how to specify the classpaths, not to mention the "classpaths" may be different on different machines or the required classes/packages may not even exist on running machines... Or am I missing something??? Should I use something like Launch4J to ease this problem?

As a newbie, I have limited vision of what the "real projects" out there are like, so it would be good if you can show me some real world project example with explanation. Truly appreciated, really. This question is really bugging me since the first day of my Java learning.

S
8 years ago
Ivan Jozsef Balazs, thank you. That rang my bell!
9 years ago

Winston Gutkowski wrote:

S Chan wrote:The reason that I want to keep the properties file outside the Jar, is that after deployment, I would still like to use a text editor to edit/view the properties file in order to change certain behaviour of the program. Does that make good sense? Or is that not wise to do so?




Why not give us a bigger picture of what you want to achieve, rather than how you want to do it?

Winston



True. I get what you mean. Thank you for the reply.

I am using the properties files to store a lot of configuration values for the program.

For instance in one part of the program, I need to find a piece of thrid party/external software in the running machine to see if that software exists. If so, launch it when user wants so. I want to include the default location of the launcher/executable of the software. So I just thought, since I am having firiends who have different OSs, from WIndows, Mac to different GNU Linux distro, it would be good if the users can modify the properties for the first time and forget about the trouble the next time they run the program. Since most of them have Windows machines, I think it would be good to include this in the properties file:
launcher.location=C:\\Program Files\\The Software\\run.exe
and let them modify the value manually if they need to.

Also, another thing is that, I want to have my program to store/save certain settings as the users change these settings through GUI. I think the easiest way to read/write these changed settings is using properties files. Am I right? Although the setting-saving frequency may not be high, the setting-reading is vital everytime the Jar runs. I don't think it would be wise to modify/write the properties file if it is inside the Jar. I don't want to do anything about writing files inside the packaged Jar when the Jar is running. It would be messy and I doubt if that is even possible.

Let me know if there are better solutions than this. I would appreciate it very much.
9 years ago
Thank you Anayonkar for the fast reply.

The reason that I want to keep the properties file outside the Jar, is that after deployment, I would still like to use a text editor to edit/view the properties file in order to change certain behaviour of the program. Does that make good sense? Or is that not wise to do so?

I thought about using adding classpath or env variable, but a problem is that if I distribute the jar(along with the properties file) to other people. Those people would have to need to add classpath or env variable on their machines before the program could function properly. Am I right at this point? I would prefer not to specify any classpath or env variable so that the users would not need to do that and run the Jar on different machines portably and without certain admin/system permissions.

To be frank, I wish to distribute the whole thing in a ZIP file or any other archive file formats. Inside the ZIP file, there contains a Jar and some directories containing properties files and some resource files. When the users receive the ZIP file, they can extract everything into one directory and run the Jar directly.

If i were to use the third method, using the relative location from current Jar. How should I set that up for an IDE like Eclipse? what is that "relative location" for the IDE workspace?
9 years ago
Hello all,

I am a newbie and currently involved in this project which requires me to load an external properties file. I have Googled a bit and saw quite a few different ways of achieving this, but none has satisfied what I want.

My project would ultimately deploy as a Runnable Jar, so I need a way to load an external properties file with a path relative to the Runnable Jar's location. Also, another problem is that, at development stage, I have to put the properties file inside the src folder of the Eclipse workspace, otherwise, Eclipse won't be able to locate the properties file.

How should I put/set up the properties file so that:
1. When I go File>Export>Runnable Jar file on my eclipse, the exported Jar will not include the properties file inside;
2. The properties file is readable/accessible while I am developing/debugging in eclipse environment
3. The properties file is accessible with relative path while the exported Jar is still running
4. There is no need to change in code and hard-coded path between development in exporting.

Last but not least, since I am just a newbie, I would really appreciate if you can provide me a few lines of demo code meeting my requirements above.

Thank you.
9 years ago

Winston Gutkowski wrote:

S Chan wrote:I have a Panasonic KX-P1121 Dot Matrix Printer here in the company. I have to write a Java app that creates print jobs for this printer. The problem is, it is a dot matrix printer which I have no experience of, and I believe it is very different from a InkJet and Laser printer in terms of resolution, font, etc...

Anyone has experience with printing with dot matrix printer using Java? Do you know if there are any online articles I can read about this? Anything pitfall I should be aware of?


Well, assuming you have all the drivers for it, I imagine it would be just like printing to any other printer (although, from the looks of it, setting the page size and/or font may be a bit trickier). This sounds to me like an OS issue, not a Java one.

The same site also has a FAQ page that suggests that it is compliant up to Windows 7 with an Epson LQ driver, but don't ask me how to set it up. All I can say is that once you can actually print to it from a regular program (eg, Word), I suspect it should print just fine from Java.

However, one might ask why your company is wasting time and money for you to write Java code to print to a printer which is so old that it's probably worth less than an hour of your wages.

Winston



We have to use that dot matrix printer because it uses pins to stamp on the paper with pressure rather than spraying ink on the paper. The need of this is that we use copyless copy paper, so that the printouts can be signed on with pen, creating several copies of the signature.

We have the driver already and thank you for your finding. Our printing is actually working with some software already but they are written in C/C++. My current task is to create some supplementary applications that hopefully can also use the same printer.
9 years ago
Hello all,

I have a Panasonic KX-P1121 Dot Matrix Printer here in the company. I have to write a Java app that creates print jobs for this printer. The problem is, it is a dot matrix printer which I have no experience of, and I believe it is very different from a InkJet and Laser printer in terms of resolution, font, etc...

Anyone has experience with printing with dot matrix printer using Java? Do you know if there are any online articles I can read about this? Anything pitfall I should be aware of?

Thank you all for help.

9 years ago

Darryl Burke wrote:1. Do you know how to get all the installed Fonts? If not, go to the API for java.awt.Font, click "Use" at the top of the page, and look for a method with a return type of Font[]. Then go through the API for that method and other methods of the class it's in.

2. Do you know how to determine if a Font can display a specific String? If not, find the appropriate method in the Font API.

You can iterate over the available Fonts to identify those that are usable for the purpose, and provide an error message if none are found.



I know that using canDisplayUpTo can determine if a Font can display a specific String.

However, I don't think iterating a list of system fonts is a good way of doing it. For instance, a typical Taiwanese Traditional Chinese Windows XP system has about 70 default fonts installed but among these, there are only 3 fonts that is able to display Traditional Chinese. It can be very inefficient to loop through the list to display every string for each component. That's my biggest concern for why I did not choose this method in the beginning.
9 years ago

Paul Clapham wrote:I don't understand why specifying a useful font would cause a "huge increase of file size" compared to automatically falling back to that same useful font. If it were my application and I expected to be supporting CJK scripts, I would just use a font which supported them.


May be I am not understanding your message very well but let me explain my situation.

A font that covers only the Western European character sets would probably be around than 50kb or fit comfortably under 100kb, but an usual font that covers complete CJK character sets would probably be a few megabytes or even measures in 10s of megabytes.

If I embed a few font files to cover the basic Latin character set, my application can easily fit under 1Mb, but if I embed a few CJK compatible fonts, my application would become 10s of megabytes in size.

If I don't embed the CJK font files and let the application use the client system's fonts, my application could save a lot of file space while keeping the interface usable in another language.
9 years ago
Hi all,

I am doing some i18n work on my swing application. At the moment, the application works perfectly in English. I have embeded a few font files in the application so that all text displayed in English will use those fonts. I do it like this:


The problem is that those font files only support basic Latin character set. My application will not show the correct text if it is trying to display other languages like CJK(Chinese-Japanese-Korean). To prevent an huge increase of file size, I wish not to change the font to another font which supports larger character set. Instead, I hope the application will fall back to the default Swing/system font which can display the text correctly.

How should I change my code to handle that? Nevertheless, I hope it is not going to be a big change, because I have all over everywhere in different custom GUI-generating classes.

I thank you in advanced.
9 years ago

Rob Spoor wrote:No, but you really have to know how to use it. It's easy to write buggy user interfaces with it.



I won't say I am a master of GridBagLayout, but I have read the related javadocs and understand them. I think I, to some extend, *know* how to use it
9 years ago

Ranganathan Kaliyur Mannar wrote:I use GBL, but I don't usually use padding. You should make use of the columns wisely. In this case, btnHlp will be on colunm 0, ok will be in column 2 and cancel in column 3. JTextArea will start from column 1 and its width will be 3. JLabel will start at column 0 and its width will be 4. Getting the in-between space will be harder, but I have done that earlier with filler JPanels. You should give the label and textarea with fill = HORIZONTAL so that, they will take up the extra width on offer.



Good point. I have done something similar before, but I don't understand why GridBayLayout doesn't do well for the paddings and insets.... Sometimes all paddings would just disappear by random and/or stretching. I checked all the gridx and gridy, but they are all correct. The idea behind is just like positioning something or spreadsheet, I suppose? It's shouldn't be so hard to understand and picture in mind. Sometimes all components in the JFrame just flashes and moves very quickly like crazy... Don't know what happened.

Is GridBagLayout buggy?
9 years ago

Nam Ha Minh wrote:Look at the GridBagContraints.fill, GridBagContraints.anchor, GridBagContraints.weightx, GridBagContraints.weighy... they are very useful when working together. I have been using GridBagLayout for years and it fits all my needs from simple to complex GUI layout.



I appreciate your suggestions, but I am already having them all over everywhere. I just can't imagine how a pretty form can be setup nicely with GridBagLayout without using these GridBagConstraints
9 years ago

Michael Dunn wrote:> people who have good understanding and experience of GridBagLayout

well, that would be (virtually no-one) very few people

> As I have always heard that GridBagLayout is the most flexible layout manager in Swing,

'flexible' is not the first description that comes to mind

from your description, and pic, some nested panels, each with a differing layoutManager,
should handle it all comfortably. You've described what is to take the extra space when resized-up,
you will also need to handle resize-down.



I guess having many sub-level layoutManager is the easiest way to achieve it... but that would probably lose the point of using GridBagLayout in the first place

Can the problem of resize-down ease off by having Min size of JFrame?
9 years ago