Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

Simple Java program runs in NetBeans IDE but not when built

Good Afternoon Stackoverflow users.

I have created a simple java program that reads a file, stores each line and then allow the user to edit a certain part of the line and saves the file in another place.

This all works fine in the NetBeans IDE 11.2. I can run the project and it runs as expected and allows me to edit the files etc.

When I go to Clean and Build the project it creates the folders and gives me a .jar file along with other source files.

When I try to run the .jar file (windows 10 pro) outside the IDE it just flashes the CMD screen and then closes instantly. So I'm not sure if the issue is with my system and the JRE or, the program itself?

I am running a freshly installed JRE8 and NetBeans is linked to JDK13.

I have tried to create a bat file to run and I get the same result instant closure of the program.

I have included the simple program below:

package PaymentReferenceEditor;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.showMessageDialog;

public class Main 
{ 
    public static void main(String[] args) 
    {    
        try 
        {
            BufferedReader reading = new BufferedReader(new FileReader("Z:\BGCDATA.in"));
            int lines = 0;
            while (reading.readLine() != null) lines++;
            reading.close();
            
            FileReader reader = new FileReader("Z:\BGCDATA.in");
            BufferedReader bufferedReader = new BufferedReader(reader);

            FileWriter myWriter = new FileWriter("C:\temp\bacs\BGCDATA.out");
            
            int l = 1;
            String c1;
            String c2;
            String c3;
            String c4;
            String c5;
            String line;
            String es;
            int el = lines -4;
            int cn;
            int tc;
            JFrame frame = new JFrame();
 
            while ((line = bufferedReader.readLine()) != null) 
            {
                if(l > 0 && l < 5)
                {
                    myWriter.write(line + "
");
                }
                if(l > 4 && l < el)
                {
                    c1 = line.substring(0,46);
                    c2 = line.substring(46,64);
                    c3 = line.substring(64,82);
                    c4 = line.substring(82,100);
                    
                    cn = l - 4;
                    tc = el - 5;
                    
                    c5 = c4 + '
' + c1 + ' ' + c2 + '
';
                    
                    System.out.print(cn);
                    System.out.print(" of ");
                    System.out.println(tc);
                    System.out.println(c5);

                    String s = (String)JOptionPane.showInputDialog(
                    frame,
                    cn + " of " + tc +"
"
                    + c5,
                    "FloPlast Limited",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    null,
                    null);
                    
                    if ((s != null) && (s.length() > 0) && (s.length() < 15)) 
                    {
                        showMessageDialog(null, c5 + " " + s );
                        //write line to file
                        es = String.format("%-18s",s);
                        es = es.toUpperCase();
                        myWriter.write(c1 + c2 + es + c4 +  "
");
                    }
                    else
                    {
                        showMessageDialog(null, c5 + " " + c3 );
                        //write line to file
                        myWriter.write(c1 + c2 + c3 + c4 + "
");
                    }
                    
                    //showMessageDialog(null, c5);
                }
                if(l > el)
                {
                    myWriter.write(line + "
");
                }
                l++;
            }
            reader.close();
            myWriter.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        System.exit(0);
    }   
}

This problem really has left me scratching my head. I have checked file association and the JAR is linked to the java file.

Any help would be appricated.

question from:https://stackoverflow.com/questions/65902830/simple-java-program-runs-in-netbeans-ide-but-not-when-built

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...