954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Beginner problem compiling with javac

Hi I am trying to compile a program from a tutorial I am doing.
The program is titled myprogram.java and is located in the c:\src> directory.
When I use the command c:\src>javac myprogram.java I get the following errors.
myprogram.java:1: expected
class
^

myprogram.java:8: '>' expected
^
2 errors

and when I try
c:\src> javac src\myprogram.java
I get:
error: cannot read: src\myprogram.java
1 error

I am using windowsXP and jdk1.5.0_06
I have set Path and CLASSPATH in my enviromental variables.
I am sure this is a simple problem, but if anybody would help I would really appreciate it.
Thanks.

paul221
Newbie Poster
2 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

Can you post the code as well?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Can you post the code as well?


Hi thanks for the reply. my source code was:
class myprogram {

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

but as soon as i changed it to
public class myprogram {

public static void main(String[] args) {
System.out.println(
"Eureka, I can put Java on my resume.");
}
}
it worked!
Now my new problem when I type
c:\src>java myprogram
i get
Exception in thread "main" java.lang.NoClassDefFoundError: myprogram
Thanks again.

paul221
Newbie Poster
2 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

So let me get this straight. Are you having problems with compilation and execution i.e setting it to the correct directory,or problems with the actual program (syntaxing 'n' stuff)?

:sad:

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
i get Exception in thread "main" java.lang.NoClassDefFoundError: myprogram Thanks again.

Did you save the file as myprogram.java ? Could be your classpath is not correctly set.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

I think server_crash is correct that it is a classpath problem.

My guess is the following command would work for you:

java -cp . myprogram

hooknc
Posting Whiz in Training
219 posts since Aug 2005
Reputation Points: 11
Solved Threads: 8
 

probably a classic case of not adding the current directory to the classpath.
"java -cp . myprogram" should do the trick until you learn to use proper naming conventions for your classes and members.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

by default when u install jdk, the execution path is set to jdk1.5.../bin
.try compiling and executing your java programs in that particular directory.
this should definitely work.good luck.

indianscorpion2
Junior Poster in Training
82 posts since May 2005
Reputation Points: 9
Solved Threads: 1
 

bad idea. What you're saying is "don't learn to use your tools, here's a quick hack for the terminally lazy and stupid".

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
bad idea. What you're saying is "don't learn to use your tools, here's a quick hack for the terminally lazy and stupid".

I know that my idea is a little obvious.but paul here is just a beginner and he is just concerned with compiling his first java program for god's sake.so I just gave him a simple advice to use the default directory.

indianscorpion2
Junior Poster in Training
82 posts since May 2005
Reputation Points: 9
Solved Threads: 1
 
by default when u install jdk, the execution path is set to jdk1.5.../bin .try compiling and executing your java programs in that particular directory. this should definitely work.good luck.


I must admit from a nubee's point of view that would appear the most intuitive way to get your programs working. I did exactly same when I was trying to jar up my work.

However, it only has a short term benefit. Eventually, he will need to learn how to do it properly. Since this is obviously a strong reason, showing him the proper way to configure his paths may prove more beneficial. Tee he he.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

In the command prompt you can also type (once you've changed directory to the directory where the file you're trying to compile is located):
set classpath=.
Now try compiling and running again :)
Also you might consider naming any classes you write with a capital letter, that is, renaming your file to Myprogram.java and changing the first line in your code to
public class Myprogram
I believe this is the 'proper way' to name classes.

InNeedOfHelp
Newbie Poster
4 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You