Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java inconsistance run experience

Status
Not open for further replies.

PROFESSORSPARKIE

Instructor
Oct 24, 2002
181
US
I have a weird problem in java. I am relatively new to java programming but have a lot of experience in several other languages & and need some guidance.

Below you see the commands to compile and run a java program, I wrote for command batch execution for a class of mine.

It all works on my home system with a beautiful report but on three other systems, at school, I have tested it on it fails as below. All of the systems are set up in almost identical structure with java installed in a JAVA_RUN folder. I take the program & data from computer to computer on a flash drive withe same failure except at home.

G:\JAVA>C:\JAVA_RUN\BIN\JAVAC G:\JAVA\Inv_rpt.java
(no return message)

G:\JAVA>C:\JAVA_RUN\BIN\JAVA G:\JAVA\Inv_rpt
Exception in thread “main” java.lang.NoClassDefFoundError: g:\java\Inv_rpt

I am looking for some guidance to solving this problem ASAP because I am on a deadline.

Here is the program listing:

//********************************************************************
// INVENTORY REPORT PROGRAM IN JAVA Author: Prof. Morris Sparks
//
// DEMONSTRATES THE READING OF A CHARACTER TEXT FILE
// USING A STANDARD INPUT DATA STREAM.
//********************************************************************
// THE INPUT DATA RECORD FORMAT IS:
// ---------------------------------
// part_id from actual input columns 1- 6 start relative 0
// part_desc from actual input columns 7-26 start relative 6
// unit_cost from actual input columns 27-30 start relative 26
// unit_sell from actual input columns 31-34 start relative 30
// on_hand from actual input columns 35-38 start relative 34
// on_order from actual input columns 39-42 start relative 38
// order_point from actual input columns 43-46 start relative 42
//
// THE OUTPUT PRINT RECORD FORMAT IS:
// ----------------------------------
// part_id from input record
// part_desc from input record
// on_hand from input record
// on_order from input record
// order_point from input record
// unit_cost from input record
// unit_sell from input record
// total_cost from calculation on_hand * unit_cost
// total_sell from calculation on_hand * unit_sell
// order_amount from calculation order_point - (on_hand + on_order)
// with selection (on_hand + on_order)/order_point>.50
// profit_ratio from calculation unit_sell / unit_cost
// with selection of < 1.32
//*********************************************************************
import java.io.*;
import java.util.StringTokenizer;
import java.util.Date;
import java.text.DecimalFormat;
//import java.util.Calendar;

public class Inv_rpt
{
//-----------------------------------------------------------------
// Reads data about a store inventory from an input file,
// creating an array of InventoryItem objects, then prints them.
//-----------------------------------------------------------------
public static void main (String[] args)
{

// **********************************************
// * Define the required variables & work areas *
// **********************************************

String Myfile = "\\JAVA\\inv_rpt.txt";
String line;
String part_id, part_desc, unit_cost, unit_sell;
String on_hand, on_order, order_point;
String Print_work, hold;
String work_pad = " ";
int uct, ust, oht, oot, opt, oh, oo, op;
int page_num = 1;
int work_length, work_size;
DecimalFormat fmt1, fmt2, fmt3, fmt4;
float total_cost = 0, total_sell = 0;
float final_cost = 0, final_sell = 0;
float uc, us, profit_ratio, order_amount, per_cent;
float work1, work2, work3, work4, work5;
float page_total_cost = 0, page_total_sell = 0;
float final_total_cost = 0, final_total_sell = 0;
Date current_time;

// set up hex-char strean to printer for landscape mode - 27 38 108 49 79
// set up hex-char strean to printer from landscape mode - 27 38 108 48 79
// byte b1 = 27, b2 = 38, b3 = 108, b4 = 49, b5 = 79;
byte b1 = 0x1B, b2 = 0x26, b3 = 0x6C, b4 = 0x31, b5 = 0x4F;

fmt1 = new DecimalFormat("####");
fmt2 = new DecimalFormat("00.00");
fmt3 = new DecimalFormat("##,###,##0.00");
fmt4 = new DecimalFormat("0.00");
current_time = new Date();

work_size = work_pad.length();

// **********************************************
// * DEFINE & OPEN THE INVENTORY DATA FILE. *
// **********************************************

try
{
FileReader fr = new FileReader (Myfile);
BufferedReader inFile = new BufferedReader (fr);
line = inFile.readLine();
// **********************************************
// * PRINT REPORT HEADINGS ON FIRST PAGE. *
// **********************************************

// System.out.print(b1 + b2 + b3 + b4 + b5);
System.out.print(" ");
System.out.print(" SPARKS CORP.");
System.out.println(" ");


System.out.print(" ");
System.out.print("INVENTORY STATUS REPORT");
System.out.println(" PAGE " + page_num);

System.out.print(" ");
System.out.print(" " + current_time);
System.out.println(" ");

System.out.print ("===================================================");
System.out.println("===================================================");
System.out.print(" PART PART ");
System.out.print(" ON ON ORDER UNIT UNIT");
System.out.println(" TOTAL TOTAL ORDER PROFIT");

System.out.print(" NO DESCRIPTION ");
System.out.print(" HAND ORDER POINT COST SELL");
System.out.println(" COST SELL POINT RATIO");
System.out.print ("===================================================");
System.out.println("===================================================");

// ************************************************
// * LOOP PROCESS TO READ EACH INPUT DATA RECORD. *
// ************************************************
while (line != null)
{
try
{
part_id = line.substring( 0, 6);
part_desc = line.substring( 6,26);

unit_cost = line.substring(26,30);
uct = Integer.parseInt(unit_cost);
work1 = uct;
work1 = work1 / 100;

unit_sell = line.substring(30,34);
ust = Integer.parseInt(unit_sell);
work2 = ust;
work2 = work2 / 100;

on_hand = line.substring(34,38);
oht = Integer.parseInt(on_hand);
work3 = oht;

on_order = line.substring(38,42);
oot = Integer.parseInt(on_order);
work4 = oot;

order_point = line.substring(42,46);
opt = Integer.parseInt(order_point);
work5 = opt;

total_cost = work1 * work3;
page_total_cost = page_total_cost + total_cost;
final_total_cost = final_total_cost + total_cost;

total_sell = work2 * work3;
page_total_sell = page_total_sell + total_sell;
final_total_sell = final_total_sell + total_sell;

final_cost = final_cost + total_cost;
final_sell = final_sell + total_sell;

order_amount = opt - (oht + oot);
per_cent = ((work3 + work4) * 100) / work5;

profit_ratio = work2 / work1 ;
//*********************************************************************
System.out.print(part_id + " ");
System.out.print(part_desc + " ");

Print_work = fmt1.format(oht);
hold = work_pad + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-5+1,work_size);
System.out.print(Print_work + " ");

Print_work = fmt1.format(oot);
hold = work_pad + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-5+1,work_size);
System.out.print(Print_work + " ");

Print_work = fmt1.format(opt);
hold = work_pad + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-5+1,work_size);
System.out.print(Print_work + " ");

Print_work = fmt2.format(uct);
hold = work_pad + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-6+1,work_size);
System.out.print(Print_work + " ");

Print_work = fmt2.format(ust);
hold = work_pad + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-6+1,work_size);
System.out.print(Print_work + " ");
//*********************************************************************
Print_work = fmt3.format(total_cost);
hold = work_pad + "$" + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-13+1,work_size);
System.out.print(Print_work + " ");
//*********************************************************************
Print_work = fmt3.format(total_sell);
hold = work_pad + "$" + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-13+1,work_size);
System.out.print(Print_work + " ");
//*********************************************************************
if (per_cent > 50.0)
{System.out.print(" ");}
else
{Print_work = fmt1.format(order_amount);
hold = work_pad + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-5+1,work_size);
System.out.print(Print_work + " ");
System.out.print(" ");
}
//*********************************************************************
if (profit_ratio < 1.32)
{System.out.print(fmt4.format(profit_ratio) + " ");}
else
{System.out.print(" ");}
//*********************************************************************
System.out.println(" *");

}
catch (NumberFormatException exception)
{
System.out.println ("Error in input. Line ignored:");
System.out.println (line);
}
line = inFile.readLine();
}

//*********************************************************************
// print page totals
System.out.print(" ");
System.out.print("PAGE TOTALS => ");
Print_work = fmt3.format(page_total_cost);
hold = work_pad + "$" + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-13+1,work_size);
System.out.print(Print_work + " ");
Print_work = fmt3.format(page_total_sell);
hold = work_pad + "$" + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-13+1,work_size);
System.out.print(Print_work + " ");
System.out.println(" ");
//*********************************************************************
// print final totals
System.out.print(" ");
System.out.print("FINAL TOTALS => ");
Print_work = fmt3.format(final_total_cost);
hold = work_pad + "$" + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-13+1,work_size);
System.out.print(Print_work + " ");
Print_work = fmt3.format(final_total_sell);
hold = work_pad + "$" + Print_work;
work_size = hold.length();
Print_work = hold.substring(work_size-13+1,work_size);
System.out.print(Print_work + " ");
System.out.println(" ");

inFile.close();

}
catch (FileNotFoundException exception)
{
System.out.println ("The file " + Myfile + " was not found.");
}
catch (IOException exception)
{
System.out.println (exception);
}
}
}



Computer thought: I teach a lot of programming so I can learn. You can never learn it all.
 
To jaxtell,

I thought I tried that also and made sure that when I did specify the path that I used the proper case on each letter knowing that java was looking at the parameter string and
not Windows Command. I was at work when I sent the help message and came home to see that it worked with out the path but failed with the path.

I have always taught students, you get into less trouble if you always specified a fully qualified name to a file or program, over riding any forgotten or unknown defaults. But Java is a very controlled case environment like my Linux host site.

I guess I don’t understand how java seemed to find the program but according to the message he failed to find the “main” point.

Thanks a lot. I am always learning!!!!

Computer thought: I teach a lot of programming so I can learn. You can never learn it all.
 
To jaxtell,

I thought I tried that also and made sure that when I did specify the path that I used the proper case on each letter knowing that java was looking at the parameter string and
not Windows Command. I was at work when I sent the help message and came home to see that it worked with out the path but failed with the path.

I have always taught students, you get into less trouble if you always specified a fully qualified name to a file or program, over riding any forgotten or unknown defaults. But Java is a very controlled case environment like my Linux host site.

I guess I don’t understand how java seemed to find the program but according to the message he failed to find the “main” point.

Thanks a lot. I am always learning!!!!

Computer thought: I teach a lot of programming so I can learn. You can never learn it all.
 
What I don't understand it's why it's working in one machine: Java expects a class name, not a Windows path.

I'd try
Code:
java -cp G:\JAVA\ Inv_rpt


And would take a look at this

Cheers,
Dian
 
Here I am again. I ran the program at my office as you said with out the path prefix on the file name. It failed. Worked at home not at my office, I have not had time to try it in the class room yet. I program compiles with
"javac Inv_rpt.java" with no errors and as I said it is OK at home. I dupe the command, take off the ".java" and change "javac" to "java" to execute the JVM but it fails with

G:\JAVA>C:\JAVA_RUN\BIN\JAVA Inv_rpt
Exception in thread "main" java.lang.NoClassDefFoundError: Inv_rpt

All my system are same????

Second question, in java 6 update 17 there isn't a java compiler "javac.exe".
What program is used to compile from the command line in that release and are there any special parameters?

Computer thought: I teach a lot of programming so I can learn. You can never learn it all.
 
To: Diancecht

I went to your try "this" link you sent me and it solved my problem. I need to code:

G:\JAVA>C:\JAVA_RUN\BIN\JAVA -classpath . Inv_rpt

My home system must have a value in my set area that accomplishes this to happen. I will look to night and post.

Thanks lots. Now I will try it in my class room systems

Computer thought: I teach a lot of programming so I can learn. You can never learn it all.
 
Javac and java do differnt things. The first one takes a file, so it is valid to take an absolute or relative path here - it must be valid, but needn't match the package structure.

But java expects a class, and the class has to be in the right package, and the package in either the right directory, or in a jar-file, and one of them in the classpath.

The current directory might be considered automatically in the classpath (if no CP else is given). Test that.

You have to realize, that a package c.d.e with a Class F in directory a/b is not the same as a package d.e with a Class F in a directory a/b/c
Code:
// directory: a/b/c:
package d.e;
class F {}

// directory: a/b:
package c.d.e;
class F {}

If your directory-structure is /a/b/c/d/e, File F.class has to be in directory e in both cases, and you have to place yourself - for instance - in /a/b.

In one case you call
Code:
java -cp ./c d.e.F
in the other case
Code:
java -cp . c.d.e.F

The whole package-structure is part of the name and has to be used to start the main, and inside your code.

Of course you may call it from everywhere, i.e. /a/b/c:
Code:
java -cp . d.e.F
in the other case
Code:
java -cp ../ c.d.e.F
or from /foo/bar:
Code:
java -cp /a/b/c d.e.F
in the other case
Code:
java -cp /a/b c.d.e.F


don't visit my homepage:
 
To all interested:

I think I found the answer, & worth posting for others.

In Windows there is a “SET” area which contains a table of variables for program testing/using. One of these values is “CLASSPATH”. In my home system my “SET” area does not contain a value for this variable so my “java Inv_rpt” command defaults to looking for the program in the current folder/directory but at work there is a list of folders in the variable, none of which was the one I was placing my program in so java could not find it. At home I created a value for “CLASSPATH” that did not point to my program folder and the execution that had worked failed but if I changed my execute line to “java -classpath . Inv_rpt” it worked again.

In summary it seems:

First java looks in the command and if it contains “-classpath . Inv_rpt” then the current default folder is searched.

If in the value is not on the command string then he checks the “SET” area for guidance.

Then if those tests fail then he uses the current default folder.

In other words he use the current folder unless a standard has been “SET” up is defined but if the execute command wants to, it can give a command override.

I hope this proves helpful to others.

Computer thought: I teach a lot of programming so I can learn. You can never learn it all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top