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 / General / December 2006

Tip: Looking for answers? Try searching our database.

Question:

Thread view: 
lei - 31 Dec 2006 02:20 GMT
1)If I have two classes, how can i run it in the command prompt? (ex:
java FileName) for only one class.
2)If I need an input from the command prompt, (This is required.  I
should not use any other method of getting an input) how can i, for
example, print no input if there's no input?

To illustrate:

1)class File{
  }

  class Main{
  }

c:\>javac File.java
c:\>java File

Error: Exception in thread "main" java.lang.NoSuchMethodError:main

2) sample run:

c:\>java Palindrome A Toyota
     A palindrome

c:\>java Palindrome abracadabra
     Not a palindrome

c:\>java Palindrome
     No Input!
Andrew - 31 Dec 2006 02:41 GMT
If this program isn't for a course or isn't an exercise of some sort,
the following paragraph does not really apply to you.

I'd hate to discover I was cheating you out of learning for a course by
telling you the answer, but maybe this will help you learn the
fundamentals better.

I can't tell what you're asking in question 1, have you thought about
this thoroughly? Are you trying to run both with one command?

If so, you could possibly put in the first one, the one you want to run
from the command prompt, a line telling the other class to run.

That is, if your classes were something like

    public class A
    {
         public static void main(String[] args)
         {
              System.out.println("Class A is running");
         }
    }

and an identical one with B instead of A, you could put instead for A:

    public class A
    {
         public static void main(String[] args)
         {
              System.out.println("Class A is running");
              B.main(args);
         }
    }

To see whether there was an input or not, you can do this: pretend your
code looks like the following, and has an additional
isPalindrome(String s) method.

    public class B
    {
         public static void main(String[] args)
         {
              if(args == null)
                   System.out.println("No input!");
              else
              {
                   String wholestring = "";
                   for(int i = 0; i < args.length; i++)
                        wholestring += args[i];
                   if(isPalindrome(wholestring))
                        System.out.println("A palindrome");
                   else
                        System.out.prinltn("Not a palindrome");
              }
         }
         //also there should be a boolean method
"isPalindrome(String)"
    }
Lew - 31 Dec 2006 15:06 GMT
>           public static void main(String[] args)
>           {
>                if(args == null)

More like
  if ( args.length == 0 )

To the OP: You will get better help if your subject line is a bit more
specific than "Question:", for example, "How do I invoke one class out of
several?" (Which, incidentally, is not your problem, but "how to write a
main() method and invoke the class".)

(To which the answer is
1. write a main() method according to the basic rules of Java invocation.
2. declare the class "public" - up to one public class per class file, remember!
3. invoke the class with the "java" command.
)

Sun's Java tutorials and Bruce Eckel's book _Thinking in Java_ cover these
basics quite well. Study them. It is pretty much the first topic covered.

- Lew
Andrew Thompson - 31 Dec 2006 02:44 GMT
Sub: Question:

1st point.  A title of 'Question:' is not very helpful.
This post should ideally be split into two parts, the
first titled (something like) 'Run from command prompt'.

Note that title does not mention 'question' or 'problem'
or 'help', which is almost assumed, and the body of the
message can make clear, in any case.

I will deal with only the first, on this thread.

> 1)If I have two classes, how can i run it in the command prompt? (ex:
> java FileName) for only one class.
...
> To illustrate:
>
[quoted text clipped - 3 lines]
>    class Main{
>    }

(good illustration)
The code above shows two classes File and Main,
both with no attributes, and no *methods*, let alone
a method called main (that is of the right form to
be understood as *the* main(String[] args) method
that the JVM looks for, to start a class).

> c:\>javac File.java
> c:\>java File

The JVM understands that any class to be loaded
and run has a 'main', so ..

> Error: Exception in thread "main" java.lang.NoSuchMethodError:main

..here it reports it was unable to find that 'main' method.

Google for HelloWorld in Java and it should show
a valid form for the main.

As an aside, an excellent group for those beginning
Java, is comp.lang.java.help

HTH

Andrew T.


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.