why my executable jar file created in Netbeans cant write/output a .txt file. . .my program must create a text file and write with it, how can it doesn't work when i convert it to .jar but if i run it in netbeans it work fine?..

Recommended Answers

All 17 Replies

Define what you mean by "can it doesn't work".

Do you have any errors or exception StackTraces to help us go on ?

i mean is that my program doesn't write or produce a .txt file and write with it when i coverted to executable jar file. i dont have any errors.

thanks for the reply...

Show me some code, on how you are creating your file, also the exact command how you are executing your jar file.

well, i created the program using netbeans ide 6.1 it has features that automatically create and build jar files of the programs. so i just created a script to run so that i can just double click to run it her is the code of the script..
java -jar "/home/viber/JAVAFINAL/dist/JAVAFINAL.jar"

any suggestions?

How are you creating the file ? If it is done with an absolute path then we need to see the code to tell you what might be going wrong. If it is created with a relative path in your program then the file certainly won't be created in the same place that it was created when the program was run from NB. It would be created in the directory from where you are running teh program now, i.e. your script.

Edit : Don't be shy to post your code, you have already been asked for it. It's only you who would be benefited since your query might get sorted out faster. Also if you are afraid we would steal your code, don't worry we won't. We just steal it when we have appraisals coming up and that too of the colleagues who trust us the most ;)

well. it is done by absolute path. the program runs ok in netbeans and it produce a .txt file inside my netbeans project folder(JAVAFINAL)..but when my programs convert it to .jar file using netbeans it doesn't work...

java -jar "/home/viber/JAVAFINAL/dist/JAVAFINAL.jar"

Now I see the reason for the problem. I am guessing your username is "viber" on your computer and you are running this application is user "viber" and your O.S is Unix/Linux based.

Now when you create the "txt" file what path have you hardcoded in your program ? Does it create the file in the current directory or have you hardcoded the some path inside "/home/viber", eg "/home/viber/sample.txt".
If you have not done the latter the problem is just of simple file permissions on your Linux / Unix box. By default a normal user is not allowed to create or edit files inside the "/", only the "root" user can create/edit/delete any file he wants.
If any other user in Linux wishes to create/edit/delete files in any folder other that his "home", you will need to grant him permissions on the folder explicitly using either the chown,chgrp or by adding that user specifically to some group and allowing that group to access to modify the contents of that folder.

You can learn more about file permissions in Unix here.

Alternatively you could just change your working directory to your "home" i.e. "/home/viber" and execute the JAR file as :-

java -jar JAVAFINAL/dist/JAVAFINAL.jar

it is done by absolute path. the program runs ok in netbeans and it produce a .txt file inside my netbeans project folder(JAVAFINAL)..but when my programs convert it to .jar file using netbeans it doesn't work...

thanks for the reply...
@stephen your correct my username is viber and it has permission..but still doesn't work.

note: the programs runs fine it produce a .txt file inside NB project folder name JAVAFINAL ex. /home/viber/JAVAFINAL/out.txt and the jar file is located in JAVAFINAL/dist/...

Like it has already been mentioned, post your code which can be compiled and tested to reproduce the exact problem.

now i can write the a txt file but my problem now is that the content is blank
maybe the problem is in this part of my code...

String filetoRun = file.getAbsolutePath();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
OutputStream out = new FileOutputStream("/home/viber/out.txt");
int result=compiler.run(null, null,out,"-Xmaxerrs","1", filetoRun);

how can it run fine in NB but when it converted in jar file is not running anymore?.

there is an error;......
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

maybe because of this line.
int result=compiler.run(null, null,out,"-Xmaxerrs","1", filetoRun);

Since you are already getting an exception, can you print out the exact StackTrace of what is being thrown up at you. java.lang.NullPointerException s occur when you are trying to invoke a method/access a property etc on an object reference set to Null.

For e.x. the following code snippet would throw a NullPointerException on the second line.

String alpha=null;
alpha = alpha.substring(1); // Calling a method on an object 
                            // reference set to nul

thanks you for the reply...
Yes now there is an exception when running in jar form her it is.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MAINFRAME.executeActionPerformed(MAINFRAME.java:2471)
at MAINFRAME.access$2500(MAINFRAME.java:58)
at MAINFRAME$28.actionPerformed(MAINFRAME.java:1742)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:374)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1688)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1732)
at java.awt.Component.processMouseEvent(Component.java:6106)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3276)
at java.awt.Component.processEvent(Component.java:5871)
at java.awt.Container.processEvent(Container.java:2105)
at java.awt.Component.dispatchEventImpl(Component.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2163)
at java.awt.Component.dispatchEvent(Component.java:4293)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
at java.awt.Container.dispatchEventImpl(Container.java:2149)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4293)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

note the line 2471 is the line where
int result=compiler.run(null, null,out,"-Xmaxerrs","1", filetoRun); is placed..
how come i didnt see this error running in netbeans, now as my program is converted to executable jar i see this error?....

maybe my problem is my jdk pakage install in my linuxs box?.. my java version is jdk 6.0.0 i just install it using synaptic manager..

maybe JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
is not supported in my java version?...

This is a problem of your JDK (JAVA_HOME) path not being set. Thats the reason the program doesn't get a java compiler. And if you read the ToolProvider docs, it says that the method getSystemJavaCompiler returns null if no compiler is provided.

In your Linux system, set the PATH variable to %JAVA_HOME% then export the path, by writing EXPORT PATH on the next line. For this to take effect you need to logout of your X-window system, so just do a ctrl-alt-backspace and login again, and then try to run the program.

i figured it out now and my program runs smoothly in executable jar file.
my guess is right the problem is in my java -version the class ToolProvider is not supported.

maybe because NB install in my linux boxs has latest jdk thats why i can run it w/o error..i just install NB in a BUNDLE package. NB has its own java.

what i did is i just uninstall the jdk and download latest jdk in sun download and install jdk manually
now my java -version is java version "1.6.0_12"

for those who responded to my queries a BIG THANKS especially to stephen thanks for giving your comments i really appreciated and also to moderator thanks...

sorry for my english:))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.