File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why I'm getting garbage after executing the class file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why I Watch "Why I New topic
Author

Why I'm getting garbage after executing the class file?

David Hernandez
Greenhorn

Joined: Jun 06, 2010
Posts: 13
I have a Compaq Presario F700 w/Win7 loaded (No adobe, messenger, music, very clean), my environmental variables worked (because javac work). I saved "C:\Users\name\Documents\My JAVASE\MyFirstApp.java" in notepad, I compiled it "C:\Users\name\Documents\My JAVA SE\javac MyFirstApp.java" in DOS, I ran it "C:\Users\name\Documents\My JAVA SE\MyFirstApp.class " but what I get is the strangest results, basically garbage as follows:

-------Source Code---------
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;

public class MyFirstApp
{
public static void main (String[] Args)
{
System.out.println("I Rule!");
System.out.println("The World");
}
}
-------Source Code Ends-------

-------Garbage starts-MyFirstApp.class---------
Êþº¾ 1            <init> ()V Code LineNumberTable main ([Ljava/lang/String;)V
SourceFile MyFirstApp.java      I Rule!    The World MyFirstApp java/lang/Object java/lang/System out Ljava/io/PrintStream; java/io/PrintStream println (Ljava/lang/String;)V !         *· ±    
1   ² ¶ ² ¶ ±         
-------Garbage ends--------------------------

I have not touch anything regarding printing, much less touching anything related to printing to a file. So, does anyone has an idea why this garbage gets triggered how can I see the true output? I will trully appreciate your suggestions.

Jeremy Medford
Ranch Hand

Joined: Jan 16, 2007
Posts: 44
Try this:
C:\Users\name\Documents\My JAVA SE\java MyFirstApp
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Tha "garbage" is the contents of that class file. I don't know precisely what you typed to see that, but the way to run a Java class file is to type "java" followed by the name of the class -- not the path to the class file, or the name of the class file. So cd into the directory "C:\Users\name\Documents\My JAVA SE" and type "java MyFirstClass".

Now go and read this article: HowToSetTheClasspath .

And finally, try running from some other directory using the -cp switch:

java -cp "C:\Users\name\Documents\My JAVA SE" MyFirstApp

Most of the time this last way of doing things is what you'll really use in the scripts that run your programs.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Open it with a hex editor, or better still use javap -c MyFirstApp

That will allow you to see the contents of the .class file in a more understandable format. You will recognise some of the output. Of course, if you simply want to execute your file ( ) Ernest has already told you what to do.
David Hernandez
Greenhorn

Joined: Jun 06, 2010
Posts: 13
I'm such a dork, I was reading the book very closely and noticed that just by typing "java MyFirstApp" without the quotes and without the .class, IT WORKS! It gave me the results. I guess that little thrills make me happy. I will also acknowledge that your explanation -Friedman- is better than mine.!!! Thanks
--------------------------------------------------------------------------------------------------------------
Ernest Friedman-Hill wrote:Tha "garbage" is the contents of that class file. I don't know precisely what you typed to see that, but the way to run a Java class file is to type "java" followed by the name of the class -- not the path to the class file, or the name of the class file. So cd into the directory "C:\Users\name\Documents\My JAVA SE" and type "java MyFirstClass".

Now go and read this article: HowToSetTheClasspath .

And finally, try running from some other directory using the -cp switch:

java -cp "C:\Users\name\Documents\My JAVA SE" MyFirstApp

Most of the time this last way of doing things is what you'll really use in the scripts that run your programs.
 
 
subject: Why I'm getting garbage after executing the class file?
 
Similar Threads
Head First Java - probably a really dumb question from the newest newbie ever
Inheritance - Object Class
Convert decimal value to binary
How to point PATH envirnoment to /bin directory?
Problem compile