Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Getting errors in CMD  (Read 2178 times)
0 Members and 1 Guest are viewing this topic.
Offline hansielneff

Senior Newbie


Exp: 1-3 months



« Posted 2015-05-01 17:14:01 »

I've made a game which runs perfectly fine in Eclipse, but doesn't work with CMD.

I can compile the file, but whenever I try to run it through CMD, it says "Error: Could not find or load main class Game" (Game is my main class). I tried using CMD with this simple program:

1  
2  
3  
4  
5  
public class Test() {
     public static void main(String[] args) {
          System.out.println("Test");
     }
}

And it worked. I got the output "Test" in my CMD.
(My game contains more than one class. Four, to be exact.)
Offline jonjava
« Reply #1 - Posted 2015-05-01 18:21:22 »

do you have the "public static void main(String[] args) {};" method in the class you're trying to run?

Offline davidc

Senior Devvie


Medals: 5
Projects: 2



« Reply #2 - Posted 2015-05-02 00:51:06 »

I can compile the file, but whenever I try to run it through CMD, it says "Error: Could not find or load main class Game" (Game is my main class). I tried using CMD with this simple program:
e contains more than one class. Four, to be exact.)

Is your "Game" class within a package?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hansielneff

Senior Newbie


Exp: 1-3 months



« Reply #3 - Posted 2015-05-02 08:31:59 »

My Game class contains this:
1  
2  
3  
4  
public static void main(String[] args) {
     Game game = new Game();
     game.start();
}



And yes, it's in the package "com.hansi.pong"
Offline Riven
Administrator

« JGO Overlord »


Medals: 1324
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2015-05-02 11:53:25 »

Then your main class is: com.hansi.pong.Game, not Game

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline hansielneff

Senior Newbie


Exp: 1-3 months



« Reply #5 - Posted 2015-05-02 13:12:20 »

I've tried that too.

I'm able to run this in CMD:
1  
2  
3  
4  
5  
6  
public class Test {
     public static void main(String[] args
     {
          System.out.println("Test!");
     }
}


But when I try to run this the exact same way as I do with the above program I get this: "Error: Could not find or load main class com.hansi.spaceinvaders.Game"
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
package com.hansi.spaceinvaders;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
   private static final long serialVersionUID = 1L;
   Thread gameThread;
   JFrame frame;
   
   public int width = 500;
   public int height = width;
   public String title = "Space Invaders";
   
   boolean running = false;
   
   public Game() {
      setSize(width, height);
     
      frame = new JFrame(title);
      frame.setResizable(false);
      frame.setSize(width, height);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(this, BorderLayout.CENTER);
     
      frame.pack();
      frame.setVisible(true);
   }
   
   public void start() {
      running = true;
      if (gameThread == null) {
         gameThread = new Thread(this);
         gameThread.start();
      }
   }
   
   public void stop() {
      running = false;
      if (gameThread != null) {
         try {
            gameThread.join();
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
   
   public void run() {
      while (running) {
         tick();
         render();
         
         try {
            Thread.sleep(16);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
   
   public void tick() {
     
   }
   
   public void render() {
      BufferStrategy bs = getBufferStrategy();
      if (bs == null) {
         createBufferStrategy(3);
         return;
      }
     
      Graphics g = bs.getDrawGraphics();
      //Render here
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, width, height);
     
      g.dispose();
      bs.show();
   }
   
   public static void main(String[] args) {
      Game game = new Game();
      game.start();
   }
}
Offline davidc

Senior Devvie


Medals: 5
Projects: 2



« Reply #6 - Posted 2015-05-03 00:49:14 »

What is the exact java command line you are running?

Where are the class files generated by javac? Are they sitting in a series of folders under the current location such as com/hansi/spaceinvaders?
Offline SHC
« Reply #7 - Posted 2015-05-03 01:17:12 »

Suppose you have a folder called as root, the root of your project files. If your main class has no package, then the location would be

1  
2  
3  
4  
<root> $ dir
Test.java
Test.class
<root> $

Now if your class is in a package, for example, say
com.me.mypackage
, then the location will be like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
<root> $ dir
com
<root> $ cd com
<root/com> $ dir
me
<root/com> $ cd me
<root/com/me> $ dir
mypackage
<root/com/me> $ cd mypackage
<root/com/me/mypackage> $ dir
Test.java
Test.class
<root/com/me/mypackage> $

Remember that you can only run these java classes from the root directory. So in the first case, you will call this.

1  
2  
3  
<root> $ java Test
Hello World ;)
<root> $

And even if you have any packages, you should be calling java from the first directory, i.e., root. Like this.

1  
2  
3  
<root> $ java com.me.mypackage.Test
Hello World ;)
<root> $

I hope now you understand what is wrong with your calls. Hope this helps.

Offline hansielneff

Senior Newbie


Exp: 1-3 months



« Reply #8 - Posted 2015-05-03 09:10:27 »

Thank you, SHC!
I finally got it working! Cheesy
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (21 views)
2017-11-21 16:21:00

xxMrPHDxx (14 views)
2017-11-21 16:14:31

xxMrPHDxx (16 views)
2017-11-21 16:10:57

Ecumene (117 views)
2017-09-30 02:57:34

theagentd (150 views)
2017-09-26 18:23:31

cybrmynd (260 views)
2017-08-02 12:28:51

cybrmynd (252 views)
2017-08-02 12:19:43

cybrmynd (247 views)
2017-08-02 12:18:09

Sralse (260 views)
2017-07-25 17:13:48

Archive (882 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!