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: <identifier> 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.

Can you post the code as well?

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.

Member Avatar for iamthwee

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:

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.

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

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.

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.

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".

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.

Member Avatar for iamthwee

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.