New to Java first project.

Started by
13 comments, last by KrinosX 10 years, 4 months ago

Hello,

I just started learning programming and I'm using java and I'm running into a issue getting the code to work. Here's the code I'm using:

/ /create a class named Magamar

public class Magamar

{

/ /this is the main method

public static void main (String[] args)

{

/ /this prints whatever is below.

System.out.println("Hello World")

}

}

Now I can get this code to build but not run, when I run it I run into this error:

Error: Could not find or load main class magamar.Magamar
Java Result: 1

I don't know what is wrong, can some one point out to me my error?

Advertisement

The file name needs to be Magamar.java

"Talk is cheap. Show me the code."

- Linus Torvalds

so it should be public class Magamar.java if I understand you correctly? And thank you for your reply!

Well....

you must have a file name called Magamar.java with your code.

so

( if you are using command line )

#> javac Magamar.java

( it will compile and generate a Magamar.class )

now you must run the code..

# > java -cp . Magamar

The '-cp' option tell the 'java' application where to search for .class files...

if you put '-cp .' ( in windows ) you tell the java so search the .class files in current directory... it may work...

[edited]

it occurred to me you may have put your class in a package... if so, you may have the proper folder structure, and call the 'javac' and 'java', like '# > javac your.package.structure.ClassName.java and java -cp . your.package.strucutre.ClassName

hope it helps

[/edited]

KrinosX

Sorry the book I'm using gives me those exact lines to code and I did it down to the letter but I'm not sure what the poster above me mant by the file name has to be named Magamar. I don't understand what you posted could you break it down into something simple? I can post a SS of what I'm doing but I don't know how I'm on windows 8. typing public class Magamar.java didn't work @.@ still at a lost.

You should have a file on your pc named Magamar.java

In that file should be the code you wrote in the first post.

Then in the command line you go to the folder that holds the file Magamar.java

Then you type: javac Magamar.java (this will generate a Magamar.class file)

Then you type: java Magamar

"Talk is cheap. Show me the code."

- Linus Torvalds

ok.. my english is not so good.. maybe I was not able to express me well...

lets do a step by step:

( I suppose you have the JDK installed, so you can use the 'Javac' and 'Java' commands from your command prompt. )

1) Create your class ( Magamar ): Create a file named 'Magamar.java' ( maybe in c:\javaproject, so it will be c:\javaproject\Magamar.java )

2) Edit the file and put the code into it:


public class Magamar
{
	//this is the main method
    public static void main (String[] args)
    {
	//this prints whatever is below.
	System.out.println("Hello World");
    }
}

3) open the command prompt ( windows ) or console ( linux ) and use 'cd c:\javaproject' ( you must be in the same folder as your Magamar.java file )

4) Execute the 'javac' command:

#> javac Magamar.java

( you may notice it will create a file called Magamar.class in the same directory )

5) run your program ( use 'java' command )

#> java -cp . Magamar

- it will call the java 'interpreter' ( its the java virtual machine btw ) telling to execute the Magamar.class file

You must see the 'Hello World' string on console.

Are you using some IDE to develop? or are you developing on 'notepad' or some text editor?

KrinosX

sorry I'm not catching on >.< to start off simple do I need to rewrite the code I wrote or is it another problem?


You should have a file on your pc named Magamar.java

In that file should be the code you wrote in the first post.

Then in the command line you go to the folder that holds the file Magamar.java

Then you type: javac Magamar.java (this will generate a Magamar.class file)

Then you type: java Magamar

Axel, if you dont use the '-cp' command you will get the error :


C:\>java Magamar
Error: Could not find or load main class Magamar

I think you must tell the virtual machine where to look for your class files, unless it is in your default classpath..... :)

KrinosX

I'm using JDK and Netbeans along with a book that teaches how to program using java.

This topic is closed to new replies.

Advertisement