Jump to content
  • Advertisement
Sign in to follow this  
shaadjd

New to Java first project.

This topic is 2092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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]

Edited by krinosx

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Edited by axel1994

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!