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
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
how would I write a class that is invoked by main?
// 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
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.
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
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");
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.
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
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);
}
}
Use your Rename class in wherever you are trying to integrate this functionality. Simply call:Rename rename = new Rename();rename.rename("somevalue");- Saish
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);
}
}
What are you typing at the command line? java App foo.txt- Saish
Within the directory where the CLASS file resides?- Saish
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.
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 >
