Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help with java and file directories

Help with java and file directories
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Jun 22, 2001, 01:24 AM
 
Hi. I'm doing a summer project here at school in java. I am a novice programmer and am slowly learning java in order to complete my task. I have been given a partner to work with because she expressed interest in doing a project similar to my idea.

The project is to create a napster-like client and server. It is no way as elaborate as napster and in fact we're pretty much just making a program that will act as a search engine for everyone's public folder on the network (the NT network, so Mac users will need samba or sharity.)

Anyway, my partner has been making the client and I've been doing the server. In less than two weeks I've created a pretty stable server and a terminal client to go with it that works great. The server can search quickly and I haven't been able to throw anything at it which can make it crash. It is also capable of working with lists of thousands of files (which will be necessary in the end).

The client side is a different story. This girl is a windoze user and doesn't understand how logical interfaces are supposed to look. She has everything in the wrong place. She's doing it using swing so on my machine it uses aqua. The client program is also responsible for indexing a folder of files on the user's HD and send it off to the server. This is where we're having problems. She doesn't seem to understand how path names work in any system but windows. I haven't been able to figure out myself exactly how to do this. She's using list(); to list the directory but the user has to enter the pathname to the directory. Whatever pathname I enter crashes her app and all she tells me is "It's not my fault." She could care less if the client ran on any computer but her own, but obviously I'm not going to spend the summer working on this if I can't use it (even though it probably won't be very usable since this whole summer project thing is meant to be a learning experience.)

This is a big annoyance and I was was hoping someone experienced could take the time just to tell me how to set up a method to read all of the filenames within a directory. I know list() and listFiles() are supposed to do this but the way she has it it keeps crashing.

Is this the kind of animosity towards the Mac platform that I'm going to be seeing from collegues when I go out into the real world?

Thanks.
     
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status: Offline
Reply With Quote
Jun 22, 2001, 02:23 AM
 
<font face = "courier">new File( pathname )).list()</font>

That's all there is to it. Now what is cause your crash? Well, if a directory does not exist at the specified path, this will return <font face = "courier">null</font>. Your partner is not handling this error condition, which is why her program crashes.

Make sure the separator character used in the path is appropriate. Windows uses back slashes and Mac OS X uses forward slashes. If you are constructing the path programmatically, use <font face = "courier">File.separator</font> to get the appropriate separator.

The javadocs do a pretty good job of explaining this.
     
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status: Offline
Reply With Quote
Jun 22, 2001, 02:33 AM
 
Originally posted by waffffffle:
<STRONG>Is this the kind of animosity towards the Mac platform that I'm going to be seeing from collegues when I go out into the real world?</STRONG>
I'm sure this varies depending on your profession, but from where I sit the real world is very Mac OS X friendly. Not Mac friendly, mind you, but Mac OS X friendly. Just pop open a Terminal window and that will get most technically-minded people on your side.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Jun 22, 2001, 03:59 AM
 
Thanks honeydew. I'm having trouble figuring out where the pathname starts from. Does it start from where you're running the program? The user's home folder? The root level of the hard disk? The girl had her working from C\yadayadayada.... I find the javadocs to be hard to understand since I really just started learning. Also, what does it read it into? An array?

Please forgive me since I'm new to this.
     
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status: Offline
Reply With Quote
Jun 22, 2001, 11:20 AM
 
No problem.

Here is a simple program that demonstrates directory listing. You'll notice that the return value for <font face = "courier">list()</font> is a <font face = "courier">String</font> array. I use a <font face = "courier">for</font> loop to iterate through the resulting array and print the values to standard out.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
import java.io.File;

class FolderList
{
<font color = green>static</font> <font color = green>void</font> main( String[] args )
{
String[] files = new File( args[<font color = blue>0</font>] ).list();
<font color = green>if</font>( files == null )
{
System.err.println( <font color = red>"Directory does not exist at path "</font> + args[<font color = blue>0</font>] );
<font color = green>return</font>;
}
<font color = green>for</font>( <font color = green>int</font> k=<font color = blue>0</font>; k&lt;files.length; ++k )
System.out.println( files[k] );
}
}
</font>[/code]

Compile this program and then run it with a pathname as an argument. For example, here I compile the program, and run it to list the contents of the Developer folder:

<font face = "courier">&gt; javac FolderList.java
&gt; java FolderList /Developer
.DS_Store
Applications
Documentation
Examples
Headers
Java
Makefiles
PBBundles
ProjectBuilder Extras
ProjectTypes
Tools
</font>

There are two types of paths: absolute and relative. An absolute path begins with a forward slash (/) and always refers to the same location. A relative path does not begin with a slash, and the location depends on your working directory. The working directory is the directory you are in when you start the Java program in Terminal. Type 'pwd' in Terminal to see your current directory.

For example, say my working directory is my home directory, <font face = "courier">/Users/honeydew</font>. If I specify <font face = "courier">Library</font> as the path, it will look for a <font face = "courier">Library</font> directory in my home directory. If I type <font face = "courier">/Library</font> with a leading slash, it will look for the <font face = "courier">Library</font> directory at the root of the file system, not my home directory.

Working directory: <font face = "courier">/Users/honeydew</font>
  • If I type the path <font face = "courier">Library</font>, this means the same as <font face = "courier">/Users/honeydew/Library</font>
  • If I type the path <font face = "courier">/Library</font>, it always refers to <font face = "courier">/Library</font>, regardless of the working directory

Simple enough, right? Now on Windows, absolute paths begin with a drive specifier like <font face = "courier">C:</font>, not a slash. And as I said before, Windows uses the black slash (\) rather than the forward slash (/) inside the path.

So let's say there is a folder called <font face = "courier">tmp</font> on the root level of both your Windows and Mac OS X machines. Inside this folder is another folder called <font face = "courier">test</font>. How do you write an absolute path to refer to this folder?

Mac OS X: <font face = "courier">/tmp/test</font>
Windows: <font face = "courier">Ctmp\test</font>

Okay, one more detail: the backslash character has special meaning, so when you type a Windows path in your code, you have to escape it using a second backslash. So to finish up the example:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
String macPath = <font color = red>"/tmp/test"</font>;
String winPath = <font color = red>"C\tmp\\test"</font>;
</font>[/code]

Good luck.


Edited about 5 times to fix UBB mistakes.

[ 06-22-2001: Message edited by: honeydew ]
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Jun 23, 2001, 04:48 AM
 
Thank you so much. This is so extremely helpful. This is exactly what I need to know, it's perfect. Thanks again.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Jun 23, 2001, 05:20 AM
 
Thanks again. This is perfect. But I've got one more question for you, if you don't mind.

I've got a nice little client now that indexes a folder and sends that array, line by line, to a server.

I'm trying to get the server to read in those lines to a new array. In order to create an array you need to know the size of it ahead of time right? So if I'm sending the files line by line, and the server needs to load the lines into an array before actually knowing how big that array is, how can it do it?

I was thinking of making the client send the size of an array over ahead of time, which would be good except I can't get an int off a readLine. Is there an easy way to turn a string into an int?

Thanks so much. You are extremely helpful.
     
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status: Offline
Reply With Quote
Jun 23, 2001, 12:29 PM
 
Originally posted by waffffffle:
<STRONG>I was thinking of making the client send the size of an array over ahead of time, which would be good except I can't get an int off a readLine. Is there an easy way to turn a string into an int?</STRONG>
You are on the right track.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
String line;

<font color = brown>// ...</font>

try {
<font color = brown>// string -&gt; <font color = green>int</font> can throw NumberFormatException</font>
<font color = green>int</font> arrayLen = Integer.parseInt( line );

<font color = brown>// add error check to insure arrayLen is non-negative</font>

String[] arry = new String[ arrayLen ];

<font color = brown>// read remaining lines into arry</font>
<font color = brown>// ...</font>
}
catch( NumberFormatException nfe ) {
<font color = brown>// should never happen, but just in <font color = green>case</font>..</font>
System.err.println( <font color = red>"expected an <font color = green>int</font>, found "</font> + line + <font color = red>" instead"</font> );
}
</font>[/code]
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Jun 23, 2001, 01:19 PM
 
Why not use a Vector instead of an array??
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Jun 26, 2001, 08:53 PM
 
Honeydew, I'd like to thank you again for all your help. But again I've reached another wall. Well actually it's partner who's hit the wall. I keep bailing her out. We've pretty much been going along and looking up things in the javadocs when we needed to do something that we haven't learned yet. That has been working out ok but now my partner, who's making the client, is telling me that one of the crucial parts to our program is not possible.

See, since we're new at this, I thought of the idea of making the software act as just a search program for the NT network because people on campus like to share their files off their hard drives in public folders. So we're making the client index that public folder and the server would keep a big index of everyone's public folders and the client could submit search queries. The results are going to be sent back to the client and the client was supposed to be able to click on a result which would promt the OS to access that file over the network.

My partner is now telling me that this isn't possible since you can't put hyperlinks in java programs. I thought that this was a feature that java had built into it. Is there a way to link to a file like that? Even if you couldn't tell the OS to transfer the file, but just to open it off the remote machine, it would be just as good.

Thanks so much.
     
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status: Offline
Reply With Quote
Jun 26, 2001, 10:19 PM
 
Hmm. I don't have a simple answer to this question.

Let me see if I understand correctly. In response to a query, the client will display a list of files. These files reside inside public folders of machines on the NT network. When the user clicks on one of these files, the file opens in the appropriate viewer. For example I click on a .mpe file and it opens in Media Player.

Yes?

The problem here is determining the appropriate helper application. I haven't done much Windows programming so I'm not sure if there is a trick to doing this. Maybe you could always open the file with explorer.exe or iexplore.exe and let those programs determine the helper application for you.

You can use methods in the <font face = "courier">Runtime</font> object to launch an external application. Let's say we want to open ctmp\test.mpe in Internet Explorer.

<font face = "courier">Runtime.getRuntime().exec( new String[] { "iexplore.exe", "c\tmp\\test.mpe" } );</font>

That might work. It will run iexplorer.exe with ctmp\test.mpe as the argument. My hunch is that Internet Explorer will attempt to load this file and launch the appropriate helper application if necessary.

Once again, my Windows skills are a bit rusty, but I think you can specify remote files as well. Instead of 'c:', start out the path with '\\' followed by the machine name and the share directory. So if there is a machine on the Windows network called 'pc01' sharing the test.mpe file out of its public folder, the path would look like this:

<font face = "courier">\\pc01\public\test.mpe</font>


I don't know of a straightforward way to solve your problem, but I wouldn't call it impossible. Good luck.
     
Mac Elite
Join Date: Sep 2000
Location: New York
Status: Offline
Reply With Quote
Jun 26, 2001, 10:34 PM
 
OK, well how about instead, the server would output the results to an html file, and would sent a message to the client and the client would direct IE (hopefully on any platform) to that web page. Then all the files would already be hyperlinks, and clicking on them would just start a download right in IE, right? Would that work? It;s not the nice little neat client that I had in mind but it may take care of what we want it to do.
     
Dedicated MacNNer
Join Date: Apr 2001
Location: San Francisco, USA
Status: Offline
Reply With Quote
Jun 27, 2001, 06:05 PM
 
Well the problem is you can't just say "launch Internet Explorer". Is IE the user's preferred browser? Is IE even available on this computer? Where is the executable located? On the Mac we have things like Internet Config to take care of this, but these services are not available to pure Java applications.

If you are only running on Windows, you can probably assume IE is present and the executable is <font face = "courier">iexplore.exe</font>. However this is a hack at best and will break on other platforms. Your safest bet may be to ask the user to locate a web browser application using a <font face = "courier">FileDialog</font>. You can save the web browser path in a config file and use it whenever you need to launch the browser. Come to think of it, I think I used this solution for a similar problem a few years ago.


Edit: I found my Java code written back in 1997 (!) for opening files in an external web browser. Hopefully it still works. Email me if you're interested and I'll send you a copy.

[ 06-27-2001: Message edited by: honeydew ]
     
ids
Fresh-Faced Recruit
Join Date: Apr 2001
Location: UK
Status: Offline
Reply With Quote
Jun 29, 2001, 07:57 AM
 
You may not need to launch a browser to show an HTML file at all ..

Have a look at the javax.swing.text.html.HTMLEditorKit, which can be turned quite quickly into a handy platform independent browser.
As to launching the file that is specified the Runtime.exec should do quite nicely, but both platforms MAC and Windows should respond quite well to you just invoking the document. You might need to prefix it with a generic command to kick it off though..its worth trying to just launch the document name as though it were a program. If that does not work on windows then the command you need is "start" and on MacOS X its "open". The runtime will need to decide what platform its on to choose the correct one
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2