Can you write a java program which does not have main but it should be executable with out any errors and exception?
Think for a while before read this post....
Yes, You can write a runnable java program which does not have main method at all. This can be done using the static block of the class.
The reason this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
Code Here:
class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}
Note: Please try the above code with command prompt.
Note: Please try the above code with command prompt.
Error in executing the code.
ReplyDelete1 I created a file with name MainMethodNot and copied the code and tried to execute it in eclipse and unfortunately i didnt found any run as java application in it.
2. Tried using command propmpt and as for compilation it had done fine and while executing i got the error.
can you figure it what might be the error
C:\folder\Desktop>java MainMethodNot
Error: Could not find or load main class MainMethodNot
Thanks in advance
Hi Harish,
ReplyDeleteThe code will not executed in eclipse, I have updated my post by mentioning this.
I think the problem what your facing is not because of the java code.Please check once your class path. Try to run another code with main method.
see this post : http://stackoverflow.com/questions/7485670/error-could-not-find-or-load-main-class
C:\Users\harish\Desktop>java
ReplyDeleteUsage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
-hotspot is a synonym for the "server" VM [deprecated]
The default VM is server.
-cp
-classpath
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D=
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:...|:]
-enableassertions[:...|:]
enable assertions with specified granularity
-da[:...|:]
-disableassertions[:...|:]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:[=]
load native agent library , e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:[=]
load native agent library by full pathname
-javaagent:[=]
load Java programming language agent, see java.lang.instrument
-splash:
show splash screen with specified image
See http://java.sun.com/javase/reference for more details.
C:\Users\harish\Desktop>javac MainMethodNot.java
C:\Users\harish\Desktop>java MainMethodNot
Error: Could not find or load main class MainMethodNot
C:\Users\harish\Desktop>
Thanks in advance
Harish
Hi, Can you try this
ReplyDeletejava -cp /your/classpath MainMethodNot
sorry frnds
ReplyDeleteThe actual error is in my system as none of the java program is running
I tried the program in another system and worked well
Thanks,