Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp



Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
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
Sheriff
Posts: 24215
37
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
Posts: 53720
127
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!