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
How Trojan Horses or Worms affect a computer system? What guidelines should be adopted to troubleshoot along with malicious software? Trojan horses are stand-alone programs. Th

Synchronous messaging involves a client that waits for the server to respond to a message. So if one end is down the whole communication will fail.

What is the difference between a while statement and a do while statement? A while statement checks at the starting of a loop to see whether the next loop iteration should occu

Polymorphism - defines the ability of a single variable of a provided type to be used to reference objects of different types, and automatically call the method that is specific

Problem : Compiler Design - Limit the loops Rajni is a newbie to the programming and while learning the programming language he came to know the following rules: ???• Each program

This project focuses on demonstrating the understanding of data types, input/output, sequential and selection programming statements, and mathematical operations. The project re

Write a program called Drivers that displays information about Formula 1 drivers and their teams.     The program starts by prompting (asking) the user for the name of an input tex


i want a full program with test about the following: linkedComputers: We will assume that a number of methods are required. These are specified below. String getComputerName() ret

What is required to be done from my programming is : I enter name,day(saturady or sunday),time( 10AM and 10PM) My program must then vaildate name,day and time. Name must only a