Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
David Hernandez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
C:\Users\name\Documents\My JAVA SE\java MyFirstApp
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic