View Full Version : Java miscompatibility

12-15-2011, 04:34 AM
I wrote a java program for a random number generator:

import java.io.*;
import java.util.Scanner;
import java.util.Random;
import javax.swing.JOptionPane;

public class Main {

public static void main(String[] args) throws IOException {
Random dc4 = new Random();
int freq[] = new int[5];
for(int roll = 0; roll < 1000; roll++){
FileWriter w = new FileWriter(new File("frequencies.rck"));
for(int face = 1; face < freq.length; face++){
w.append(face + "\t" + freq[face] + "\n");
int yn = JOptionPane.showConfirmDialog(null, "Done. Show roll stats?", "Done", JOptionPane.YES_NO_OPTION);
if (yn == JOptionPane.YES_OPTION){
Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
String r0 = rraw.next();
String r1 = rraw.next();
String r2 = rraw.next();
String r3 = rraw.next();
String r4 = rraw.next();
String r5 = rraw.next();
String r6 = rraw.next();
String r7 = rraw.next();
String r8 = rraw.next();
String r9 = rraw.next();
JOptionPane.showMessageDialog(null, r0 + ": " + r1 + "\n" + r2 + ": " + r3 + "\n" + r4 + ": " + r5 + "\n" + r6 + ": " + r7 + "\n" + r8 + ": " + r9, "Frequencies", JOptionPane.PLAIN_MESSAGE);
public static void Gen(){
int count = 0;
Random dc4 = new Random();

while (count == 0){
String xt = JOptionPane.showInputDialog(null, "How many numbers?");

int x = Integer.parseInt(xt);
for (int ct = 0; ct < x; ct++){
int d4raw = dc4.nextInt(4);
int d4 = d4raw + 1;
String d4o = Integer.toString(d4);
JOptionPane.showMessageDialog(null, d4o, "Random Number", JOptionPane.PLAIN_MESSAGE);
int ns = JOptionPane.showConfirmDialog(null, "All done. New set?", "Done!", JOptionPane.YES_NO_OPTION);
if (ns == JOptionPane.YES_OPTION){
count = 0;
count = 1;

JOptionPane.showMessageDialog(null, " Recording Data;\n Hit \"Okay\" to continue", "Done!", JOptionPane.PLAIN_MESSAGE);


and then I wrote a .bat file to run it:

@echo off
copy Main.class C:\Users\Public\Downloads\src
cd C:\Users\Public\Downloads\src
set path=%path%;C:\Program Files (x86)\Java\jre7\bin
java Main

In theory, both should have worked on any computer that has JRE7. None work on a computer that has not been configured to run Java by me.
How can I make a universally operable .bat (or if needed .exe; I am learning C++) file to make it go?

Don't complain about the script. I know it is inefficient (but there is a reason for the Scanner import, I just haven't coded it in yet), and would appreciate it if you could help me make the proof of randomness thing work more efficiently, but it works, so if all you are going to do is complain, don't. No offence to the community, but that's all I have received from people that examine the script.

12-15-2011, 04:21 PM
set path=%path%;C:\Program Files (x86)\Java\jre7\bin

I would guess that is the issue. Try changing that to

C:\Program Files\Java\jre6\bin

and see what you get.

12-16-2011, 12:43 AM
Except, then it will fail if they have jre7. Isn't there a try... catch code I can write for all of the versions of JRE or JDK?

12-16-2011, 04:46 AM
Ideally it would be like minecraft, which for those who don't play, uses a .exe file to load several .jar files that make up the game. If you run a java-based server for something, you will also probably notice a .exe file is used to call the .jar file. This will work on any computer. Thoughts or suggestions?

I'm willing to spend some time on this, as I need to learn C++ anyway and I will be able to edit it for use with most any script I write (correct?).

Ideally (above the previous "ideally," but to the best of my knowledge impossible) I would be able to find a download of System Development Kit (I clarify because Google uses that interchangeably with SE Development kit) for windows to convert it to a .exe file, instead of being called from one. Am I wrong about this being impossible? If so, can you provide a link?

12-16-2011, 04:45 PM
what about multiple .bat files and then check for the jre? quick and dirty-

check for file path (JRE7)-
if exists Runtime.getRuntime().exec("cmd /c start JRE7.bat");
else if check for file path (JRE6) Runtime.getRuntime().exec("cmd /c start JRE6.bat");

12-16-2011, 10:08 PM
I am looking for something like Executor to turn it into a .exe file. I figure that if I get good, and do enough, that I need custom favicons, I'll be making money off it and be able to justify software that costs money. I can't find a free version of something like Executor, though. Just a trial version of that.

12-17-2011, 02:57 AM
I found a free version of Executor. It took me since I posted that, but I found a "trial version" that only reminds you to register in the cmd window for the output. It basically invokes JRE or JDK for the .jar file that you made with it. In essence, what alykins said, but more user-friendly and less "dirty."

12-17-2011, 09:59 AM
Does java not create link files to the java executable into the system32 directory? Mine appears to have java, javaw and javaws with the same date and time as the install, so these are either linked or copied. This should simply let you call java.exe on the cli without any worries of its location. You shouldn't explicitly look for a location to execute; you can determine the Java version its running using java.version from System.getproperty. Use this to terminate the application if it doesn't fulfill a requirement.
Then pack this as a .jar file. .jar is cross platform compatible, so you needn't worry about being stuck on a windows machine with a .bat.