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
What is the difference between instance and object of a class? Few says both are same, then why java kept both the words for same thing?

With publish or subscribe message passing the sending application or client establishes a named topic in the JMS broker or server and publishes messages to this queue. The receivin

Explain Data Updates issues in data management? DATA UPDATES: Old titles must be removed regularly Data changes every time Prices change Transportation / shipping cost change

REQUIREMENT: We are seeking a simple tool that will convert an existing flash file (.SWF) into HTML5. Our purpose is to convert small flash games into html5 games. Please NOTE: We

What is the difference between an if statement and a switch statement? The if statement is used to select between two alternatives. It uses a boolean expression to decide whi

Develop E-Commerce Website I need an E-Commerce website for my furniture shop which requires to an online store to buy products online. So, Skills required: HTML, Java, Gr

Why Processing An Unknown Number Of Parameters ? Most of the time you have a fairly high-quality idea of what parameters will and won't be passed to your applet. Therefore som

#comparison of merge sort and bubble sort

I want Hall Rental Website Project Description: I want to prepare website for my party venue where i will post the photos and detail of place along with availability calendar

program to find the frequency of a digit in a number