473,890 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.class vs. .java

11 New Member
The moderator posted this in another thread:
(I am starting a new one not to confuse the other subject at hand)

".java classes are compiled to into .class files. If you have a .java file called Product.java, when you compile it a .class is created for every class in the file Product.java if there are no errors. In this case you are getting this error because there is no class file called Product.class so you must compile the Product class first."

I don't really understand how to do this. When I go to the Command Prompt and type in the instructions given to create the .java file it creates a .class file, but then when I try to run it, it doesn't work and I get the same error.

This is the info I have and am not sure if it all goes in the Java file or in two separate files:
//Product1 class

public class Product1 {

private String title;
private double item;
private double stock;
private double price;

//Constructor to initialize product information
public Product(String title, double item, double stock, double price) {

setTitle(title) ;
setItem(item);
setStock(stock) ;
setPrice(price) ;

}

// Method to get title
public String getTitle() {
return title;
}

// Method to get item number
public double getItem() {
return item;
}

// Method to get stock
public double getStock() {
return stock;
}

// Method to get price
public double getPrice() {
return price;


}

// Method to set title name
public void setTitle(String title) {
this.title = title;
}

// Method to set item number
public void setItem(double item) {
this.item = item;
}

// Method to set stock available
public void setStock(double stock) {
this.stock = stock;
}

// Method to set price
public void setPrice(double price) {
this.price = price;
}

// Method to compute value of inventory
public double getInvValue() {
return this.stock *this.price;
}

}//end class Product1

//Inventory program

public class Inventory
{
public static void main (String args [])
{

System.out.prin tln ("Inventory of DVD Movies:\n");

String title = "Notebook";
double item = 1;
double stock = 27;
double price = 19.00;
double value;

Product product = new Product (title, item, stock, price);

System.out.prin tf("%s%18s\n", "DVD Title:", product.getTitl e() );

System.out.prin tf("%s%16s\n", "Item #:", product.getItem () );

System.out.prin tf("%s%8s\n", "Number in Stock:", product.getStoc k() );

System.out.prin tf("%s%18s\n", "Price:", product.getPric e() );



System.out.prin tf("%s%9s\n", "Inventory Value:", product.getInvV alue() );

}

}//end class Inventory


Thanks! ITQUEST
Feb 14 '07 #1
3 2139
hirak1984
316 Contributor
How much I remember you got a noclassdeffound exception right?

Let me tell you that it appears when you use methods from a class which is not in the same package.
either you have to keep them in the same package, or give the fully qualified path of the class.
In this case it seems that your product1 class is not converted into class file,that is why main could not find the mothods you have called from main.

Do one thing compile Product1 class separately(keep ing in a separate .java file).Hope it will work.
Good luck.
Feb 14 '07 #2
r035198x
13,262 MVP
The moderator posted this in another thread:
(I am starting a new one not to confuse the other subject at hand)

".java classes are compiled to into .class files. If you have a .java file called Product.java, when you compile it a .class is created for every class in the file Product.java if there are no errors. In this case you are getting this error because there is no class file called Product.class so you must compile the Product class first."

I don't really understand how to do this. When I go to the Command Prompt and type in the instructions given to create the .java file it creates a .class file, but then when I try to run it, it doesn't work and I get the same error.

This is the info I have and am not sure if it all goes in the Java file or in two separate files:
//Product1 class

public class Product1 {

private String title;
private double item;
private double stock;
private double price;

//Constructor to initialize product information
public Product(String title, double item, double stock, double price) {

setTitle(title) ;
setItem(item);
setStock(stock) ;
setPrice(price) ;

}

// Method to get title
public String getTitle() {
return title;
}

// Method to get item number
public double getItem() {
return item;
}

// Method to get stock
public double getStock() {
return stock;
}

// Method to get price
public double getPrice() {
return price;


}

// Method to set title name
public void setTitle(String title) {
this.title = title;
}

// Method to set item number
public void setItem(double item) {
this.item = item;
}

// Method to set stock available
public void setStock(double stock) {
this.stock = stock;
}

// Method to set price
public void setPrice(double price) {
this.price = price;
}

// Method to compute value of inventory
public double getInvValue() {
return this.stock *this.price;
}

}//end class Product1

//Inventory program

public class Inventory
{
public static void main (String args [])
{

System.out.prin tln ("Inventory of DVD Movies:\n");

String title = "Notebook";
double item = 1;
double stock = 27;
double price = 19.00;
double value;

Product product = new Product (title, item, stock, price);

System.out.prin tf("%s%18s\n", "DVD Title:", product.getTitl e() );

System.out.prin tf("%s%16s\n", "Item #:", product.getItem () );

System.out.prin tf("%s%8s\n", "Number in Stock:", product.getStoc k() );

System.out.prin tf("%s%18s\n", "Price:", product.getPric e() );



System.out.prin tf("%s%9s\n", "Inventory Value:", product.getInvV alue() );

}

}//end class Inventory


Thanks! ITQUEST
These classes are both public classes. So each one must go into its own .java file. i.e You can only have one public class per .java file and the name of that .java file must be the same as the name of that public class. When you go to the command prompt, you do not create .java files there. You create .java files by simply saving a file with the .java extension. You can type a program in notepad or textpad and save it as a .java file, thus creating the .java. When you go to the command and type javac FileName.java, you are now compiling the .java file. If the file is a valid java source file (no errors) then you get class files created otherwise errors will be shown on the prompt and no files are created. You can read a bit more about this in the Java classes on this page.
Feb 14 '07 #3
ITQUEST
11 New Member
Thanks to both of you for the explanations. They made sense to me and I was able to correctly complete my assignment because of this.


Thanks Again!
ITQUEST
Feb 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
10856
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the absolute file name of a .class file eg. C:\myJava\myApp.java, then how do i translate this file name to a java class name the Object.forName method would accept has it's parameter? thanks,
19
105857
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method: System.out.print((char)27 + "[2J");
4
3431
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
1
2394
by: Andrew James | last post by:
All, I'm having some trouble with understanding python's importing behaviour in my application. I'm using psyco to optimise part of my code, but I'm not sure whether it inherits throughout the rest of my application (read this as for any imported module) if I import in in a 'higher-level' module. For example: A.py ====
14
7335
by: Mick | last post by:
I wrote a C# program that interfaces with a data vendor over the web using an API they supplied and their examples in C#. Now I have another data vendor's API and example that I want to add to my C# program. But this new API is written in Java. They gave me an example source code that uses their API but the actual API looks like it is stored in a few .jar files. I unzipped those .jar files into many .class files. I think these .class...
1
5046
by: bjwillykajilly | last post by:
Well, I got an assignment due this morning, so i guess ill end up turning it in a day late eh. anyways. I have a couple problems that I don't know what to do with. the objective is here: Write a generic class, MyStatisticsClass, with a type parameter T where T is a numeric object type (e.g., Integer, Double, or any class that extends java.lang.Number). Add a method named standardDeviation that takes an ArrayList of type T and returns as a...
1
3872
by: aapexclient | last post by:
I have a problem with Java finding my custom class directory. I have searched the forums and tried everything, but nothing seems to work. The <javapgm>.java compiled cleanly and created <javapgm>.class. Error message is: Warning: java.lang.ClassNotFoundException: javapgm Versions of software: Apache 2.0.59(Win32) PHP 4.4.4 Java 1.5.0_01 Windows 2000 5.0
2
2969
by: inetjack | last post by:
Hi, This is a little test application, generating and compiling code at runtime. The loadClassLoader() method of the Factory Object suppose to unload all class previously loaded. It does not work!!! Somebody knows why?
4
2151
by: vasavimaruthi | last post by:
hi, i got class not found exception of a small jdbc program in windows. the program is like this import java.sql.*; public class Db1 { public static void main(String a) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver");
1
13552
by: sandhyabhavani | last post by:
This article is used to zip a file or directory using vb.net. The classes and method to zip a file is availale in java.io, java.util, java.util.zip class library.To import these you have to add a reference vsjlib Library in .net component. .NET Classes used : Imports System.IO Imports java.io Imports java.util Imports java.util.zip
0
9975
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9812
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10799
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8004
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7154
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3263
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.