How to execute class with a main() method in a package, JAVA Programming

 

Example:  You have a class named "Set" in a project folder "e:\myProject" and package named com.zxy.client, will you be able to compile and execute it as it is

 

 

package com.xyz.client;

 

public class Pet {

public static void main(String[] args) {

System.out.println("I am found in the classpath");

}

}

 

To run-> e:\myProject> java com.zxy.client.Set

 

The answer is no and you would get the following exception: "Exception in thread "main" java.lang.- NoClassDefFoundError: com/zxy/client/Set". You have to set the classpath. How shall you do that? One of the following ways:

 

1. Set the operating system CLASSPATH environment variable to have the project folder "e:\myProject".

2. Set the operating system CLASSPATH environment variable to have a jar file "e:/myProject/client.jar", which has the Set.class file in it.

3. Run it with -classpath or -cp option as given below:

c:\>java -cp c:/myProject com.xyz.client.Pet

OR

c:\>java -classpath c:/myProject/client.jar com.xyz.client.Pet

 

Posted Date: 7/25/2012 12:29:37 AM | Location : United States







Related Discussions:- How to execute class with a main() method in a package, Assignment Help, Ask Question on How to execute class with a main() method in a package, Get Answer, Expert's Help, How to execute class with a main() method in a package Discussions

Write discussion on How to execute class with a main() method in a package
Your posts are moderated
Related Questions
Describe Polymorphism? Polymorphism can be referred as one name many forms. It's the ability of methods to behave differently, depending upon object who is calling it. Key feat

Describe the Edit phase- Java Program Development Phase comprises editing a file. This is done with an editor program. Programmer types a java program using editor such as no

Give an example of Class or static Members? Below is a Car class along with such a speedLimit field and getSpeedLimit() method. public class Car { private String licensePla

Want a graph database for relationship mapping of entities Project Description: I want a graph database for relationship mapping of entities, liable peoples. Also, the databa

How to draw Polygons in java? In Java rectangles are defined through the position of their upper left hand corner, their height, and their width. Therefore it is implicitly sup

This assignment has two main objectives. It aims to establish a basic familiarity with the Java API (Java Thread class) for creating multithreaded Java application, and the Java ne

How to Drawing Images at Actual Size Once the image is loaded draw it in the paint() method by using the drawImage() method like this g.drawImage(img, x, y, io) img is a

How are this () and super() used with constructors? this() Constructors: is used to pointing current class instance. Can be used with methods or variables. Used to call

Why are JSP pages the preferred API for creating a web-based client program? Because no plug-ins or security policy files are required on the client systems (applet does). Also,

There are two significant bean lifecycle methods. The first one is setup which is called when the bean is loaded on to the container. The second method is the teardown method which