What's java equivalent of C's argv[ 0 ]?

What's java equivalent of C's argv[ 0 ]?

Post by joseph_dan » Sat, 15 Apr 2006 10:51:02


I thought I knew this once, but maybe I was just fooling myself.

this.getClass().getName() doesn't work from static methods, so it
doesn't work from main().

The reason I want to do such a silly thing is that I don't want to go
looking for the bare string "MainClass" when I decide to change the
name of the MainClass to something a little more descriptive, like,
ProjectA or something. I need it to tell Mac OS X what to call the
application menu from within the program. (I know this is not what
Apple recommends.)

Checking Google dug up a thread on GCJ, with a proposed
gnu.gcj.progname property.

Can this be done in standard Java?
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by au71 » Sat, 15 Apr 2006 11:10:06

public class MyClass {
public static void main(String[] args) {
System.out.println(MyClass.class.getName());
}
}

 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by joseph_dan » Sat, 15 Apr 2006 11:25:46


erk.

I just reminded myself of why this is not the way to do things.

By the time main can install the property, Apple's runtime apparently
has already set the lousy application menu name.


I did find some good pages with this query:

http://www.yqcomputer.com/ +application+menu+name

including

http://www.yqcomputer.com/

and some of Apple's pages with very long URLs.
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Patricia S » Sat, 15 Apr 2006 13:10:52


Here's one way:

public class TestNaming {
public static void main(String[] args) {
System.out.println(new NameGetter().getName());
}
static class NameGetter{
String getName(){
return getClass().getDeclaringClass().getName();
}
}
}

This is not strictly a replacement for argv[0]. It only tells you the
name of the class in which the NameGetter is declared. For example, if I
called that main() method in another program, it would still report
"TestNaming". Each class for which you want to be able to get the name
from static contexts must declare its own NameGetter as a static nested
class.

A refactoring tool should solve the problem of name changing
without doing this sort of thing.

Patricia
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Dimitri Ma » Mon, 17 Apr 2006 03:36:58


XXXX@XXXXX.COM sez:

where ".class.getName()" is completely redundant.

To OP: main() doesn't have argv[0] for the same reason it does
not return an int. What you use if you want to change the name
of your class is the advanced refactoring technique called
"search and replace".

Dima
--
I have not been able to think of any way of describing Perl to [person]
"Hello, blind man? This is color." -- DPM
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Oliver Won » Wed, 19 Apr 2006 02:37:03


[...]

Eclipse's refactoring engine is a bit reluctant to make changes inside
the contents of string literals, though. When I want to have the name of the
class, I use something similar to the above code. Since there are no string
literals involved, the refactoring engine will "do the right thing" when you
rename your class.

- Oliver
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Steve W. J » Wed, 19 Apr 2006 02:48:22

In article < XXXX@XXXXX.COM >,




Then I needn't bother sending the reply I had planned to tell you
exactly that...

Of course, I also won't tell you that I'd never use an application named
"lousy"...(I know, I'll never make it in comedy).
--
Steve W. Jackson
Montgomery, Alabama
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Dimitri Ma » Wed, 19 Apr 2006 08:23:07

Oliver Wong sez:



Which is what makes search and replace an advanced refactoring technique:
it will work on tokens that regular refactoring engines won't touch, such
as string literals and comments.

Dima
--
Riding roughshod over some little used trifle like the English language is not a
big deal to an important technology innovator like Microsoft. They did just that
by naming a major project dot-Net (".Net"). Before that, a period followed by a
capital letter was used to mark a sentence boundary. --T. Gottfried, RISKS 21.91
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Patricia S » Wed, 19 Apr 2006 08:52:44


Eclipse rename does quite a nice job. The only reluctance I saw was that
enabling text replacement in strings and comments turns on the preview
feature, but I think I would want it anyway.

Patricia
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Oliver Won » Wed, 19 Apr 2006 22:38:34


A string-based search and replace will "do the wrong thing" if you have
MyClass as a substring of other tokens, or if you have two classes (in
different packages) called MyClass, for example. There are probably other
cases where string-based search and replace will do the wrong thing, which
is why I never use that feature, and always do a parse-based refactoring.

- Oliver
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Thomas Sch » Thu, 20 Apr 2006 00:04:20


class Whatever {
private static final Class myClass = new SecurityManager() {
public Class getClass1(){return getClassContext()[1];}
}.getClass1();
public static Class getClass1() { return myClass; }

public static final void main(String[] arg) {
System.out.println(getClass1().getName());
}

}
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by joseph_dan » Wed, 26 Apr 2006 13:00:46


It looks like it does the job for what I had in mind, if only what I
had in mind would have worked.


Maybe it's because I'm more of a C programmer (I think we once
disagreed on fine points concerning the use of ++i and i++?) and don't
see things the same way as many Java programmers, but I prefer not to
ask such things of my refactoring tools. I suppose I'm being a little
analytic compulsive about decoupling, but I like my semantics bound in
the expression rather than in the use of external tools.

Much grass.

jdz
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by joseph_dan » Thu, 27 Apr 2006 10:25:48


The problems with setting the Mac OS X application menu name from
within Java code aside, I took Patricia Shanahan's suggestion a step
further and used static initialization to salt the fully qualified
class name away in a class variable without having to mention it in
main(). For the archives:

public class AMainClass
{
// Other things in the main class here ...

public static void main( final String[] args )
{
// ...
System.out.println( "main Class name: " + AMainClass.mainClassName );
}

/**
* argv[ 0 ]
* Courtesy of Patricia Shanahan on comp.lang.java.programmer:
*/
private static class NameGetter
{ public String getName()
{ // Returns the fully qualified class name.
return getClass().getDeclaringClass().getName();
}
}
//
public static final String mainClassName = new NameGetter().getName();

}
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Eric Sosma » Thu, 27 Apr 2006 23:07:08


XXXX@XXXXX.COM wrote On 04/25/06 21:25,:

Keep in mind that every Java class can have its own
`public static void main(String[])' method; by the time
your program is up and running there may be many different
main() methods lying around. The problem of figuring out
which of them is "the" main() method seems not so easy to
solve.

(No, the existence of multiple main() methods is not
a mere perversity. For example, consider the usual way
of writing Swing code that will run either as an applet
or as an application: There's a main() that gets used when
running as an application, but that is not used when the
code runs as an applet. If the code is running as an applet,
does it make sense to designate a method that's never even
called as "the" main() method?)

--
XXXX@XXXXX.COM
 
 
 

What's java equivalent of C's argv[ 0 ]?

Post by Oliver Won » Fri, 28 Apr 2006 01:41:25


I thought the standard pattern for this sort of stuff is to have your
main method create a JWindow, add the JApplet to the JWindow, and call the
init() and start() method. So either the init() or the start() method could
be considered "the" "main" method.

I wrote an RPG engine with a level editor. The engine and the editor
shared a lot of code, so it was made one big project with two "public static
void main(String[] args)" methods. One in a class called Game, and one in a
class called Editor. Depending on which one you called, you could either
design a new game, or play the game you just designed.

At work, we're writing and Eclipse plugin to do various source-code
analysis type stuff. We've got lots of "public static void main(String[]
args)" methods so that each functionality can be tested at the command line
(or within unit tests). For example, there's an entry point where you can
pass in a input filename and an output filename, and it'll parse the input
file and dump the abstract syntax tree generated as an XML document to the
output filename. This is to test that the input files are correctly being
parsed, although that isn't the "main" goal of the plugin.

- Oliver