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
Project Description: We are looking to prepare an app which allows the user (a student or an expat in a new country) to select from a list of furniture items to prepare a packag

The Java programming language is object-oriented and includes automatic garbage collection. Garbage collection is the process of reclaiming memory taken up by unreferenced objects.

Explain in brief Java applet? It is a java program. It has been designed for transmitting Java code over the internet. It's automatically executed by Java-enabled W

Write a program to add integer value? class AddInts { public static void main (String args[]) { int i = 1; int j = 2; System.out.println("i is " + i); System.out.println("

Each time an object is started in Java it goes into the area of memory named as heap. The primitive variables like double and int are allocated in the stack, if they are local inst

The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been given by the AOP Alliance in order to ensure the interoperability amo

Model-View-Controller (MVC) is a design pattern put together to help control change. MVC decouples interface from business logic and data. ? Model: The model having the core of

Complete Back end and Front End Development Project Description: This work is a part of ongoing project. The need is to prepare and integrate the web part of this project.