Jump to content

  • Log In with Google      Sign In   
  • Create Account


New to Java first project.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 shaadjd   Members   -  Reputation: 110

Like
0Likes
Like

Posted 13 November 2013 - 12:21 PM

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?



Sponsor:

#2 axel1994   Members   -  Reputation: 406

Like
1Likes
Like

Posted 13 November 2013 - 12:36 PM

The file name needs to be Magamar.java


"Talk is cheap. Show me the code."

- Linus Torvalds

#3 shaadjd   Members   -  Reputation: 110

Like
0Likes
Like

Posted 13 November 2013 - 12:54 PM

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



#4 krinosx   Members   -  Reputation: 500

Like
1Likes
Like

Posted 13 November 2013 - 12:55 PM

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, 13 November 2013 - 12:59 PM.


#5 shaadjd   Members   -  Reputation: 110

Like
0Likes
Like

Posted 13 November 2013 - 01:02 PM

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.



#6 axel1994   Members   -  Reputation: 406

Like
1Likes
Like

Posted 13 November 2013 - 01:05 PM

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, 13 November 2013 - 01:06 PM.

"Talk is cheap. Show me the code."

- Linus Torvalds

#7 krinosx   Members   -  Reputation: 500

Like
0Likes
Like

Posted 13 November 2013 - 01:09 PM

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?



#8 shaadjd   Members   -  Reputation: 110

Like
0Likes
Like

Posted 13 November 2013 - 01:12 PM

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



#9 krinosx   Members   -  Reputation: 500

Like
1Likes
Like

Posted 13 November 2013 - 01:12 PM


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



#10 shaadjd   Members   -  Reputation: 110

Like
0Likes
Like

Posted 13 November 2013 - 01:13 PM

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



#11 shaadjd   Members   -  Reputation: 110

Like
0Likes
Like

Posted 13 November 2013 - 01:18 PM

Wait I found something when I right click on the file magamar.java and select run it runs successfully, I was using the Run option on the main menu bar and when I triend using that run option it still had an error is there a difference between those two type of run commands?



#12 krinosx   Members   -  Reputation: 500

Like
0Likes
Like

Posted 13 November 2013 - 01:28 PM

Well.. if you are using NetBeans , the only reasonable way to it happens is: You was trying to run the project before you 'build/compile' it ( maybe the autobuild option is not enable or something like that ).

 

I am not a NetBeans expert ( I use Eclipse IDE ) so I dont know all the options for netbeans.. smile.png

 



#13 Kirkkaf13   Members   -  Reputation: 300

Like
0Likes
Like

Posted 13 November 2013 - 07:01 PM

I am not a Java programmer, I used Perl. I have however, noticed you have not put a semicolon after your print statement.

 

Also reading through the above posts it sounds like your file and class name must be the same. Magama and magama are not the same.



#14 zircnambi   Members   -  Reputation: 111

Like
0Likes
Like

Posted 16 November 2013 - 03:44 PM

@shaaadjd

 

Well, for what I understood you are a begginner at programming world, because that kind of question sounds like it. 

 

You surely missed something about what you read and tried to really do. 

 

I found a good site that teaches Java with Eclipse IDE: http://www.javaprogressivo.net/p/curso-completo-de-java-para-iniciantes.html

 

You should check this out, you will find it very useful while learning.



#15 krinosx   Members   -  Reputation: 500

Like
0Likes
Like

Posted 03 December 2013 - 07:52 AM

@zircnambi

 

You suggest the guy to read a Java Course in Portuguese?

 

he is from Philadelphia.. don't think he will be comfortable to read a course written in Portuguese... 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS