Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Tools / November 2004

Tip: Looking for answers? Try searching our database.

Command Line Args in Eclipse

Thread view: 
Wade - 04 Nov 2004 14:22 GMT
11042004 0715 GMT-6

I have only recently been using Eclipse to learn Java. One of my
exercises needs a command line argument. I read one of the posts about
where to find the argument however Im still not understanding how to do
this.

The exercise takes a file and concerts all the letts to AllCaps. Using
command line Im instructed to type: java AllCapsDemo TempFile.java

On the Eclipse Arguments tap I see two sections: Program and VM args. I
typed the name of a file in the program section and the produced an
error. In the VM section I get a different error, Exception in thread
'main'.

Both of these sections have a button named Variables. This brings up a
window with a string of choices. I have tried several choices but each
one provides a different error.

This is the code for the exercise:

import java.io.*;

public class AllCapsDemo {
public static void main(String[] args) {
AllCaps cap = new AllCaps(args[0]);
cap.convert();
}
}

class AllCaps {
String sourceName;

AllCaps(String sourceArg) {
sourceName = sourceArg;
}

void convert() {
try {
// create file objects
File source = new File(sourceName);
File temp = new File("cap" + sourceName + ".tmp");

// create input stream
FileReader fr = new FileReader(source);
BufferedReader in = new BufferedReader(fr);

// create output stream
FileWriter fw = new FileWriter(temp);
BufferedWriter out = new BufferedWriter(fw);

boolean eof = false;
int inChar = 0;
do {
inChar = in.read();
if (inChar != -1) {
char outChar = Character.toUpperCase( (char)inChar );
out.write(outChar);
} else
eof = true;
} while (!eof);
in.close();
out.close();

boolean deleted = source.delete();
if (deleted)
temp.renameTo(source);
} catch (IOException e) {
System.out.println("Error == " + e.toString());
} catch (SecurityException se) {
System.out.println("Error == " + se.toString());
}
}
}

Question: Would someone tell me how to do this? Advise me as to what I
am doing wrong. Thank you.

Wade
Chris McMahan - 04 Nov 2004 15:54 GMT
The program textbox is where you would type AllCapsDemo
The args textbox is where you type TempFile.java

The VM Args option is for the JVM, and is not related to running your
command line program.

Another alternative is to use a command-line in a console and type it
directly from there.

- Chris

> 11042004 0715 GMT-6
>
[quoted text clipped - 75 lines]
>
> Wade

Signature

    (.   .)
 =ooO=(_)=Ooo========================
 Chris McMahan | cmcmahan-at-one.net
 ====================================

Wade - 04 Nov 2004 16:32 GMT
11042004 0932 GMT-6

Thanks Chris.

I put that in the Program box and still get errors. The TempFile.java
is in the same folder as the AllCapsDemo.java and this is the error I
receive:
Error == java.io.FileNotFoundException: TempFile (The system cannot
find the file specified)
Tom Sardella - 04 Nov 2004 17:03 GMT
>11042004 0932 GMT-6
>
[quoted text clipped - 5 lines]
>Error == java.io.FileNotFoundException: TempFile (The system cannot
>find the file specified)

Wade,

It's taking the argument just fine, your application just can't find the
file.  You should enter the argument as "TempFile.java" and move it to
your project root directory, or change the argument to include a pathname
relative to the root.

Tom
Wade - 04 Nov 2004 17:13 GMT
11042004 1012 GMT-6

So in the Program Arguments box I should write
C:\file\location\filename?

I did that and this error returned:
Error == java.io.FileNotFoundException: C:\WebDev\Eclipse (Access is
denied)

Access denied. Odd. Im thinking this has nothing to do with file
permissions.

Wade
Wade - 04 Nov 2004 17:15 GMT
11042004 1012 GMT-6

So in the Program Arguments box I should write
C:\file\location\filename?

I did that and this error returned:
Error == java.io.FileNotFoundException: C:\WebDev\Eclipse (Access is
denied)

Access denied. Odd. I assume this has nothing to do with file
permissions.

Wade
Tom Sardella - 04 Nov 2004 17:31 GMT
>So in the Program Arguments box I should write
>C:\file\location\filename?
[quoted text clipped - 7 lines]
>
>Wade

After looking through your code, it doesn't appear that using a pathname
will work because of how it constructs a temporary filename:

   File temp = new File("cap" + sourceName + ".tmp");

So you can only test from within Eclipse with a file that is located in
your project root directory.  In my case, my Eclipse workspace is in this
folder:

   C:\EclipseWorkspace.3.0.1

and my project is named:

   TestProject

so I'm able to run this application with the file here:

   C:\EclipseWorkspace.3.0.1\TestProject\TempFile.java

and by using this as the argument:

   TempFile.java

Tom
Wade - 04 Nov 2004 17:38 GMT
110420004 1038 GMT-6

So you are saying that if I have a file structure C:\WebDev\Eclipse for
the eclipse program and C:\WebDev\Workspace\Java  for my java files,
that this is causing the problem. That in order for me to be able to do
this I must have a workspace folder inside the eclipse folder?

Wade
Tom Sardella - 04 Nov 2004 17:56 GMT
>So you are saying that if I have a file structure C:\WebDev\Eclipse for
>the eclipse program and C:\WebDev\Workspace\Java  for my java files,
>that this is causing the problem. That in order for me to be able to do
>this I must have a workspace folder inside the eclipse folder?

No, it has nothing to do with where your workspace is located in relation
to the Eclipse executable.  In my case, I do keep them separate.  The
problem is where your current working directory is when you run the
application from within Eclipse.  That's where you need to put your file.

Try putting this at the beginning of your Main, it will tell you where the
test file should go:

   File path = new File("");
   System.out.println("Working directory = " + path.getAbsolutePath());

Tom
Wade - 04 Nov 2004 18:06 GMT
11042004 1107 GMT-6

Thanks Tom. That got it. I will get the hang of this language and this
IDE. Im learning - though sometimes slowly.

Again, Thanks

Wade


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.