*
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 Spring in Action this week in the Spring 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: 24187
    
  34

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: 39425
    
  28
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why I'm getting garbage after executing the class file?