not using an array of strings

how do I write a java method without using an array of strings?

for example:

import java.io.File;

public class rename

{

public static void main(String[] args)

{

File src = new File(args[0]);

File dst = new File("c:\\temp\\temp.txt");

boolean wasRenamed = src.renameTo(dst);

}

}

I don't want to have main use an array of strings but I want rename to work as a class

[448 byte] By [cfelssjavaa] at [2007-10-1 22:30:52]
# 1

You should follow standard naming conventions and capitalize the name of your class.

No, you cannot change the signature of main(). This method is the only one that a JVM will 'find' when attempting to execute an application. It is specified by the Java Language Specification.

You are free to write other classes that are invoked by main(). However, every application needs a standard entry point.

- Saish

Saisha at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 2
how would I write a class that is invoked by main?
cfelssjavaa at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 3

// Application.java

public class Application {

static final public void main(final String[] args) {

try {

Controller controller = new Controller();

controller.go();

System.exit(0);

}

catch (Throwable e) {

e.printStackTrace();

System.exit(1);

}

}

}

// Controller.java

publc class Controller {

final public void go()

throws Exception {

// spiffy code goes here

}

}

- Saish

Saisha at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 4
furthermore, I want to be able to have an object called rename, that uses the method rename() and will allow for a single string argument.
cfelssjavaa at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 5

Wouldn't a renaming action normally require two arguments: old name and new name? However, it would be easy:

public class Application {

private Application() {

super();

}

static final public void main(final String[] args) {

switch (args.length) {

case 1:

try {

Rename rename = new Rename();

rename.rename(args[0]); // see, it's kind of silly to have same names for class and method

System.exit(0);

}

catch (Throwable e) {

e.printStackTrace();

System.exit(1);

}

break;

default:

System.err.println("Usage: java Application [new name]");

System.exit(1);

}

}

}

class Rename {

public Rename() {

super();

}

final public void rename(final String newName) {

// spiffy code goes here

}

}

- Saish

Saisha at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 6

This probably won't help to OP but in 1.5 main can also be written

public static void main(String ... args) {}

args is still an array, but if you call other main methods from within your own program it may be more convient.

Example:

Rename.main();

Rename.main("alpha");

Rename.main("alpah","beta");

Caffeine0001a at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 7
the program works, but I am trying to insert it into another application that is saying that there are no constructors for this java program, so I don' t know enough about java to explain it another way.
cfelssjavaa at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 8
If it is going in another application, get rid of the Application class. Use the Rename class. Make it 'public class' instead of simply 'class'.Use 'Rename' normally via the 'new' keyword as 'Application' demonstrates.- Saish
Saisha at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 9

This is what I have, don't understand the last post

import java.io.File;

public class App {

private App() {

super();

}

static final public void main(final String[] args) {

switch (args.length) {

case 1:

try {

Rename rename = new Rename();

rename.rename(args[0]); // see, it's kind of silly to have same names for class and method

System.exit(0);

}

catch (Throwable e) {

e.printStackTrace();

System.exit(1);

}

break;

default:

System.err.println("Usage: java App [new name]");

System.exit(1);

}

}

}

public class Rename {

public Rename() {

super();

}

final public void rename(final String newName) {

File src = new File(newName);

File dst = new File("x:\\temp\\import_uq.txt");

boolean done = src.renameTo(dst);

}

}

cfelssjavaa at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 10
Use your Rename class in wherever you are trying to integrate this functionality. Simply call:Rename rename = new Rename();rename.rename("somevalue");- Saish
Saisha at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 11

This is what I changed it to and am getting an -- Exception in thread "main" java.lang.NoSuchMethodError: main

Sorry this is my first java program

import java.io.File;

class App {

private App() {

super();

}

static final public void main(final String[] args) {

switch (args.length) {

case 1:

try {

Rename rename = new Rename();

rename.rename("c:\\felss\\charles.txt.bak"); // see, it's kind of silly to have same names for class and method

System.exit(0);

}

catch (Throwable e) {

e.printStackTrace();

System.exit(1);

}

break;

default:

System.err.println("Usage: java App [new name]");

System.exit(1);

}

}

}

public class Rename {

public Rename() {

super();

}

final public void rename(final String newName) {

File src = new File(newName);

File dst = new File("c:\\felss\\charles.txt");

boolean done = src.renameTo(dst);

}

}

cfelssjavaa at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 12
What are you typing at the command line? java App foo.txt- Saish
Saisha at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 13
yes
cfelssjavaa at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 14
Within the directory where the CLASS file resides?- Saish
Saisha at 2007-7-13 8:45:25 > top of Java-index,Administration Tools,Sun Connection...
# 15

I got it too work from the command line and I got it finally to recognize by the other program. It is not renaming the file though, it works from a command on the server that the application is running on that is calling this java method. So, I think that I am getting close, thanks for your help.

cfelssjavaa at 2007-7-20 12:17:03 > top of Java-index,Administration Tools,Sun Connection...
# 16
It will presumably only work if a drive is mapped to X: with a folder called 'temp' on the machine executing the application.Glad I could help. Best of luck.- Saish
Saisha at 2007-7-20 12:17:03 > top of Java-index,Administration Tools,Sun Connection...