Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Can't run a java program from cmp for the life of me.


  • Please log in to reply
5 replies to this topic

#1 flickdog93

flickdog93

  • Members
  • 73 posts
  • OFFLINE
  •  
  • Local time:04:55 AM

Posted 10 July 2014 - 12:24 AM

I've set environment variables, wrote multiple Hello Worlds and other sample programs and cannot get cmp to run a java program. I just get the error,"could not find or load main class". I can use IDEs, but I haven't found one I'm suited too. I hate JCreator's workspaces and all that.

 

Any help is very much appreciated.


Edited by hamluis, 10 July 2014 - 11:15 AM.
Moved from Win 7 to Programming - Hamluis.


BC AdBot (Login to Remove)

 


m

#2 Veitch

Veitch

  • Members
  • 31 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:11:55 AM

Posted 10 July 2014 - 01:27 AM

Hi flickdog93

 

What exactly did you type on the command line and how does your program look like?

Post both here.

Often it is a very simple error, like wrong declaration of the main method or wrong command line options.

 

Regardless of available IDEs it is a good idea to know how to compile code yourself. So keep trying.

I suggest looking into Netbeans or Eclipse. Both are free.

If costs don't hinder you, try IntelliJ IDEA.


Edited by Veitch, 10 July 2014 - 01:30 AM.


#3 neilbuddy

neilbuddy

  • Members
  • 42 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Rochester MN
  • Local time:05:55 AM

Posted 10 July 2014 - 12:37 PM

Are you running the it from a .jar file.  Your .jar file should have a manifest that tells java where the main class is.  If you open the jar file in something like Winrar or 7zip it should be in /META-INF/MANIFEST.MF.  If the file is not in there you should recreate the jar file with a correct manifest that points to your main class.



#4 flickdog93

flickdog93
  • Topic Starter

  • Members
  • 73 posts
  • OFFLINE
  •  
  • Local time:04:55 AM

Posted 10 July 2014 - 09:33 PM

In the command line:

 

cd c:\users\andrew\desktop\my java programs

javac triangle.java

java triangle

 

In my Triangle class:
 
import java.util.Scanner;
public class Triangle
{
    
    public static void main(String[] args)
{
   
    // initalized vars
    Scanner text = new Scanner(System.in);
    int layers = 0;
    int spaceCount = 0;
    int xCount = 1;
    String space = " ";
   
   
    // TODO, add your application code
   
    System.out.println("How tall of a pyramid do you want to make?");
    layers = text.nextInt();
    spaceCount = layers;
   
   
    for(int i=0;i<=layers;i++)
    {
    for(int s = spaceCount;s>0;s--)
    {
    System.out.print(space);
    }
    for(int x = 0;x<xCount;x++)
    {
    System.out.print("X");
    }
    System.out.println();
    spaceCount--;
    xCount+=2;
    }
    }
}
 
The program runs fine in Netbeans. I'm guessing that my environment variables are wrong.


#5 neilbuddy

neilbuddy

  • Members
  • 42 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Rochester MN
  • Local time:05:55 AM

Posted 10 July 2014 - 11:15 PM

Try renaming the java file so the "t" is capitalized.  

 

javac Triangle.java

java Triangle

 

I tested this on linux and it worked,  I don't have Java on my Windows box to test on there.



#6 Veitch

Veitch

  • Members
  • 31 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:11:55 AM

Posted 11 July 2014 - 01:19 AM

You can check if your environment variable is set with the command java -version. If you get a proper output (the JRE version) the variables are set correctly.

In your case I assume as well that you have to write and name the file with a starting uppercase letter. The file must have the same name as the class and java is case sensitive.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users