Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Code not working on other machines

  1. #1
    Junior Member Coty0010's Avatar
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Lurking
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Code not working on other machines

    Hey guys,

    I'm an intermediate programmer who has just started java programing about two weeks ago. I've since made a simple game that is buggy but runs okay, however I found that it only runs on one machine, and that is the one with jdk 1.7, the others have java 6 and run minecraft, but do not run my app, as a jar, or as a class from command line.

    I don't exactly understand this, I compiled and tested on the command line, but neither other machine runs it? Any help would be appreciated as it would be nice to run it on more than one machine without installing jdk

    I included code and build script below just in case you needed it, my editor is basically just a syntax highlighter, it's built with "build_n_clean.bat" as I see no reason to use a 400MB IDE just so it can code for me and tell me my dyslexic spelling sucks with its funny red lines!

    PS code is messy, spent more time trying to get it to work than cleaning it...
    Attached Files Attached Files


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Code not working on other machines

    It's possible that you're code uses some feature that's new to 1.7. Could you post your code (many people are wary of downloading unknown attachments) as well as any error messages you're getting when you run your program?

  3. #3
    Junior Member Coty0010's Avatar
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Lurking
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working on other machines

    Sorry, I guess I never thought of that... Now at the moment I have only access to one of the machines, the other one I tested on is my dads mac, and it's in his room with him asleep, but I dought the dump from that will say to much more...

    Here is the dump:
    E:\viniati>java -jar viniati.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: Viniati
    Caused by: java.lang.ClassNotFoundException: Viniati
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: Viniati . Program will exit.

    Here is Viniati.java
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
     
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferStrategy;
    import java.util.Random;
     
    import javax.swing.JFrame;
     
    public abstract class Viniati implements MouseListener
    {
    		public static int  a;
    		public static int  score;
    		public static char mouse_c;
    	  /************************************************************************
    	   *  Our main code!
    	   ***********************************************************************/
    	  public static void main(String[] args){
     
    		  int line_1_xe = 200;   // line 1 pos X end
    		  int line_1_ye = 200;   // line 1 pos Y end
              int delay = 100;
    		  float radius=75;
        		int[] trix = {100,50,150};
        		int[] triy =
        			{
        				100,  // point a
        				156,  // point b
        				156   // point c
        			};
        		int[] trix1 = {100,50,150};
        		int[] triy1 =
        			{
        				100,  // point a
        				48,  // point b
        				40   // point c
        			};
        		int random_place = 0;
        		int sslr = 0; // Score since count since last random number genirated
        		int color_c;
     
    		JFrame jf = new JFrame("Viniati v1.0");
    	    jf.setIgnoreRepaint( true );
    	    jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	    jf.setResizable(false);
     
    	    Canvas canvas = new Canvas();
    	    canvas.setIgnoreRepaint(true);
    	    canvas.setSize(200, 200);
     
    	    jf.add(canvas);
    	    jf.pack();
    	    jf.setVisible(true);
    	    //
    	    // Okay, now let's make a back buffer...
    	    //
    	    canvas.createBufferStrategy(2);
    	    BufferStrategy buffer = canvas.getBufferStrategy();
    	    //
    	    // Okay, now let's do some "active rendering" !
    	    //
    		Graphics graphics = null;
    		graphics = buffer.getDrawGraphics();
    		//
    		// Now, let's also init mouse code...
    		//
    		canvas.addMouseListener(new MouseListener()
    		{
    				public void mouseClicked(MouseEvent clicke)
    				{
    					mouse_c=1;
    				}
    				public void mouseEntered(MouseEvent arg0) {}
    				public void mouseExited(MouseEvent arg0) {}
    				public void mousePressed(MouseEvent arg0) {}
    				public void mouseReleased(MouseEvent arg0) {}
    				//public void mouseDragged(MouseEvent e) {}
    				//public void mouseMoved(MouseEvent e) {}
    	  	});
    		//
    		//  Last of all, let's init the random code fuction.
    		//
    		Random rand = new Random();
     
    		while(true)
    		{
    			/*
    			 *   Lets clear our new buffer
    			 */
    			  graphics.setColor(Color.black);
    	  		  graphics.fillRect(0, 0, 200, 200);
    	      		/*
    	      		 *   Create game board
    	      		 */
     
    	      		graphics.setColor(Color.gray);
    	      		graphics.fillOval(20,20,155,155);
     
    	      		graphics.setColor(Color.red);
    	      		graphics.fillPolygon(trix, triy, 3);
    	      		graphics.fillOval(52,138,95,36);
     
    	      		graphics.setColor(Color.yellow);
    	      		graphics.fillPolygon(trix1, triy1, 3);
    	      		graphics.fillOval(47,22,100,40);
     
    	      		graphics.drawOval(20,20,155,155);
    	    		/*
    	    		 *
    	    		 */
    	    		if(mouse_c == 1)
    	    		{
    	    			if(random_place==1)
    	    			{
    		      			if(a==4)
    		      			{
    		    				score++;
    		    				delay--;
    		    				sslr++;
    		      			}
    		      			else
    		      			{
    		      				if(a==5)
    		      				{
    			    				score++;
    			    				delay--;
    			    				sslr++;
    		      				}
    		      				else
    		      				{
    		    					score=0;
    		    					delay=100;
    		    					sslr=0;
    		      				}
    		      			}
    	    			}
    		      		else
    		      		{
    			    		if(a==2)
    			    		{
    			    			score++;
    			    			delay--;
    			    			sslr++;
    			   			}
    			   			else
    			   				if(a==3)
    			   				{
    		    					score++;
    		    					delay--;
    			    				sslr++;
    			    			}
    			    			else
    			    			{
    			   						score=0;
    			   						delay=100;
    			   						sslr=0;
    			   				}
    			    		}
    	    		}
    	    		mouse_c =0;
     
    	      		/*
    	      		 *  delay exicution for a short time...
    	      		 */
     
    	      			try {
    						Thread.sleep(delay);
    					} catch (InterruptedException e) {
    						break; // I don't really care if we don't sucseed at sleeping...
    					}
    	      			if(a==6)
    	      				a=0;
    	      			else
    	      				a++;
    				/*
    	      		 *  p
    	      		 */
    	      		line_1_xe = (int) (100 + radius * java.lang.Math.cos(a));
    	      		line_1_ye = (int) (100 + radius * java.lang.Math.sin(a));
     
    	      		/*
    	      		 *  And now draw our line...
    	      		 */
    	    		graphics.setColor(Color.green);
    	    		graphics.drawLine(100, 100, line_1_xe, line_1_ye);
     
    	    		/*
    	    		 * Right the score on the board
    	    		 */
    	    		graphics.setColor(Color.white);
    	    		graphics.drawString("Score: " + score, 1, 10);
     
    	    		/**************************************************************
    	    		 * Check if Score Since Last Random generation == 4, if so
    	    		 * genorate a new number to select what zone to hit.
    	    		 *************************************************************/
    	    		if(sslr==4)
    	    		{
    	    			random_place= rand.nextInt(3);
    	    			sslr=0;
    	    		}
    	    		/**************************************************************
    	    		 * If the number genorated it 1 then do zone yellow.
    	    		 *************************************************************/
    	    		if(random_place==1)
    	    		{
    	    			if(score>=15)
    	    			{
    	    			color_c = rand.nextInt(2);
    		    			if(color_c==1)
    		    				graphics.setColor(Color.red);
    		    			else
    		    				graphics.setColor(Color.yellow);
    	    			}
    	    			else
    	    				graphics.setColor(Color.yellow);
    		    		graphics.drawString("yellow zone", 134, 190);
    	    		}
    	    		/**************************************************************
    	    		 * If the number genorated is anything else do zone red...
    	    		 *************************************************************/
    	    		else
    		    	{
    	    			if(score>=15)
    	    			{
    	    			color_c = rand.nextInt(2);
    		    			if(color_c==1)
    		    				graphics.setColor(Color.yellow);
    		    			else
    		    				graphics.setColor(Color.red);
    	    			}
    	    			else
    	    				graphics.setColor(Color.red);
    		    		graphics.drawString("red zone", 134, 190);
    		    	}
    	    		/**************************************************************
    	    		 * Last of all update the screen buffer...
    	    		 *************************************************************/
    	    		if( !buffer.contentsLost() )
    	    			buffer.show();
    			}
     
    	  }
     
    }

    manifest.txt
    Main-Class: Viniati

    my build script:

    java viniati.java
    jar cvfm Viniati.jar manifest.txt Viniati.class Viniati$1.class

    Last of all, my build and clean script:

    rem Viniati$1.class
    rem Viniati.class
    rem Viniati.jar
    java viniati.java
    jar cvfm Viniati.jar manifest.txt Viniati.class Viniati$1.class
    Last edited by Coty0010; April 2nd, 2012 at 10:30 PM. Reason: forgot manifest.txt

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Code not working on other machines

    I don't see anything wrong with the source, though it doesn't make much sense to declare Viniati as an abstract class.

    I did notice a few issues with your build script, though:

    You're using java instead of javac. java is the runtime environment while javac is the compiler. I'm guessing this is a posting issue, though, as you did say it works on some computers.

    Are you positive that it's the exact same jar file being run on both computers instead of somehow an incomplete jar file being run on one machine? Did you try unzipping the problematic jar file to determine it's contents? Have you tried running the plain java class (not in a jar file) on both machines to verifies that it works?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Code not working on other machines

    Use a zip utility program to look in the jar file and see if the class files are there and are on the correct path and have the correct names.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member Coty0010's Avatar
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Lurking
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working on other machines

    Quote Originally Posted by helloworld922 View Post
    I don't see anything wrong with the source, though it doesn't make much sense to declare Viniati as an abstract class.
    Oh, that was there for something I was trying out with the mouse... (I had a bit of a head scratcher trying to get it to work)

    Quote Originally Posted by helloworld922 View Post
    I did notice a few issues with your build script, though:

    You're using java instead of javac. java is the runtime environment while javac is the compiler. I'm guessing this is a posting issue, though, as you did say it works on some computers.
    Opps, no, I'll be honest, I don't use that script, I've been typing commands into the prompt manualy so that a year from now I know the commands and aren't sifting through docs, I just got in a hurry when I created it for this post...

    Quote Originally Posted by helloworld922 View Post
    Are you positive that it's the exact same jar file being run on both computers instead of somehow an incomplete jar file being run on one machine? Did you try unzipping the problematic jar file to determine it's contents?

    Yes, same jar, I've copied my entire viniati folder onto my pen drive, it's curently the only folder on it, && @Norm: and when I unzip the jar the only difference I find is that java edited my manifest file, but I assume that is normal...

    Manifest-Version: 1.0
    Created-By: 1.7.0_03 (Oracle Corporation)
    Main-Class: Viniati

    Quote Originally Posted by helloworld922 View Post
    Have you tried running the plain java class (not in a jar file) on both machines to verifies that it works?
    Yes, I have tried running it as a class to with no luck... There could be a problem though, on my development machine I am runing java7 with windows 7 home. Along with jdk 1.7.somethingorother

    I launch my class file like this and it works "java Viniati", maybe older ones are more strict there???
    Last edited by Coty0010; April 3rd, 2012 at 09:35 AM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Code not working on other machines

    Is the Viniati.class file in the jar file at the root level or in a folder?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member Coty0010's Avatar
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Lurking
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working on other machines

    @Norm: It is at root. Just like the jar, tf flag displays:

    META-INF/
    META-INF/MANIFEST.MF
    Viniati.class
    Viniati$1.class

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Code not working on other machines

    I can't see what is wrong. The java command is finding the Main-Class: entry and getting the name of the class.

    What version of java are you using? Is the the same as the JDK that created the class file?
    Try: java -version
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member Coty0010's Avatar
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Lurking
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working on other machines

    On my dev machine it is: java version "1.7.0_03"
    on my dads mac it's: 1.06_29
    On the other machine (my brothers) it is like Version 6 Update 31, I know because he updated it last night when we were trying to get it to work, but I can't check it right now because he uses it for school... (If it's not one thing it's another )
    Last edited by Coty0010; April 3rd, 2012 at 11:45 AM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Code not working on other machines

    If you compile with 1.7 then it will not work on 1.6 (unless you tell javac to create 1.6 code)
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    Coty0010 (April 3rd, 2012)

  13. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Code not working on other machines

    Java 7 should be backwards compatible with Java 6 for the most part (both source and binary).

    Oracle has published a list of incompatabilities that details known issues transitioning between the two. As far as I can tell, I didn't find any binary incompatibilities which would cause this problem.

    Did you try running the plain java class file on one of these other computers to determine that it is indeed the jar causing the problem, not the program?

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,350
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Code not working on other machines

    Java 7 should be backwards compatible with Java 6
    Not sure what that means.
    Java 1.7 uses a new version code in the .class file that 1.6 does not recognize.

    Compile a source with 1.7 and execute with 1.6 will give you this error:
    java.lang.UnsupportedClassVersionError: TestCode6 : Unsupported major.minor version 51.0
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Code not working on other machines

    Oops, my bad. Forgot that backwards compatibility doesn't work both ways. Yes, you're right that it does need to be compiled with the correct version information. The safest way is to compile it with JDK 1.6, though as Norm pointed out it is possible to tell javac 1.7 to cross-compile for a 1.6 runtime.

  16. #15
    Junior Member Coty0010's Avatar
    Join Date
    Apr 2012
    Posts
    8
    My Mood
    Lurking
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Code not working on other machines

    Ahah! That fixed it, I decided just to downgrade to jdk 1.6, since I'd imagine many people still use, and my java all in one desk reference is second addition not third, so it uses java 6 instead of 7 anyway (got it for $11 instead of $40!)... Probably why I didn't know they weren't upwards compatible.

    I only now have an issue making a jar... but I'll probably figure that out within the next 4 minuets or so...

    Thanks allot!

Similar Threads

  1. Why isn't this code working?
    By tai8 in forum What's Wrong With My Code?
    Replies: 33
    Last Post: March 12th, 2012, 04:23 PM
  2. I don't get why this code isn't working
    By tai8 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: February 20th, 2012, 12:38 PM
  3. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 07:17 AM
  4. How to Find machines connected to a network
    By JavaPF in forum Java Networking Tutorials
    Replies: 1
    Last Post: June 18th, 2011, 09:47 AM
  5. How to Find machines connected to a network
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 18th, 2011, 09:47 AM