Solved

java programming

Posted on 2012-03-21
12
426 Views
Last Modified: 2012-03-21
I am just starting to learn java programming and have a website with a tutorial/walk-through on learning how to code in java.
I downloaded jdk-7u3-windows-x64.exe and it installed without a problem, I am working on a windows 7 box.
I'm at a point where I am trying to set my Path and ClassPath, so I go to the environment variables and create a new variable for java. I have a screen shot.
javabut at a command prompt, if I type
java
it works fine, but if I type
javac
I get
"'javac' is not recognized as an internal or external command,
operable program or batch file."
I think this might be related to the error I get when I try to run the first "program" the tutorial gives. here is the code

public class MyProgram {

  public static void main(String[] args) {
    System.out.println(
      "Eureka, I can put Java on my resume.");
  }
}

It compiles fine if at the command prompt I go to the javac path,
C:\Program Files\Java\jdk1.7.0_03\bin
and it makes
MyProgram.class
but when I run
java MyProgram.class
I get
"Error: Could not find or load main class c:\javacode\MyProgram.class"
I don't get what I'm doing wrong?
Any ideas?
0
Comment
Question by:JeffBeall
  • 8
  • 2
  • 2
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37749289
Please post exactly your commands and in what folders you excute the,

What is c:\javacode and why this folder is mentiooned if you are doing everuthing in

C:\Program Files\Java\jdk1.7.0_03\bin

 folder?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37749295
Normally you should make sure that C:\Program Files\Java\jdk1.7.0_03\bin is in your PATH

(not javapath but PATH)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37749300
Best of all make it so that your PATH
begins with

C:\Program Files\Java\jdk1.7.0_03\bin
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37749307
Then go to any folder, copy over to that folder your file

MyProgram.java

and then type


javac MyProgram.java

and then

java MyProgram

it shouild work
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37749313
Mind, that after you change PATH in your co ntrol-panel environement you need to open a new dos window (after youchanged the PATH to opne window - not before)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37749382
This is the best practice way of setting things:


a. define JAVA_HOME as C:\Program Files\Java\jdk1.7.0_03
b. If your only jdk, append the following to PATH
%JAVA_HOME%\bin

If you need a classpath at all, it's usually better to do that at the beginning of your session
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Expert Comment

by:for_yan
ID: 37749399
The main thing is to pre-pend this :
C:\Program Files\Java\jdk1.7.0_03\bin
to your PATH

You want to perepend it as in majority of cases you'll have some older java in the path and if it comes before it can screw up your oepration

Some special cases require JAVA_HOME but it is rather rare.
The most important thisng is to have your PATH correct
(and there is no such thing as javapath - it is not used - you may remove it)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 37749465
Some special cases require JAVA_HOME but it is rather rare.

It's required where best practice is expected ;)

When you change the jdk, all you need to do is change JAVA_HOME, which is easier and less error-prone than editing PATH (which you can't even see fully in Windows most of the time)
0
 
LVL 1

Author Comment

by:JeffBeall
ID: 37749481
"What is c:\javacode and why this folder is mentiooned"

I thought I would put all the so called programs in one place. so I made a folder called javacode on the root of c

I thought I just had to make a new environment variable.
So, I put
C:\Program Files\Java\jdk1.7.0_03\bin
in the path statement, and now javac works.

and I closed all my command prompts, deleted MyProgram.class, re-compiled MyProgram.java
then ran
c:\javacode\java MyProgram.call
but I still get

Error: Could not find or load main class MyProgram.class
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37749493
you need to go to

c:\javacode>

and from that prompt say:

java MyProgram

(without any extension)
0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 37749505
so after you have set up your path correctly put your .java file to any folder, say c:\javacode

gotto that folder and say

c:\javacode> javac MyProgram.java


c:\javacode> java MyProgram


you need .java extenxsion in compilation commad

you DO NOT NEED .class exetnsion in the java command
0
 
LVL 1

Author Closing Comment

by:JeffBeall
ID: 37749529
It was the .class extension!!
I ran

java MyProgram

and it worked fine!
Thank you for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 73
Java asynchronous logging 4 50
servlet filter example 37 42
Bartender label printing - switch on and off graphics 3 10
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now