Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
public class Main{
public static void main(String args[]) {
System.out.println("Hello, World");
String name = "John";
System.out.println(name);
int myNum = 18;
System.out.println(myNum);
}
}
I run the Run command. It's a custom command:
Code:
javac "%f"; java Main
The output:
Code:
javac "Main.java"; java Main (in directory: /home/devel/java/First)
Hello, World
John
18
Compilation finished successfully.
OK.
So I changes the code:
Code:
public class Main{
public static void main(String args[]) {
System.out.println("Hello, World");
String name = "John";
System.out.println(name);
int myNum;
System.out.println(myNum);
}
}
I suppress the value of myNum to see how Java handles uninitialized values.
The output:
Code:
javac "Main.java"; java Main (in directory: /home/devel/java/First)
Main.java:9: error: variable myNum might not have been initialized
System.out.println(myNum);
^
1 error
Hello, World
John
18
Compilation finished successfully.
It points out the error, but it still prints the old content including the no longer valid value 18.
Can that behavior be changed? I don't want to see that old value or, even better, print nothing beyond the error message.
That is a random value. Occasionally it can be identical to the old value (as long as it is not overwritten), but otherwise unpredictable. Try to close geany completely, run a browser or something new app and restart it again. I'm sure that 18 will be changed.
And yes, you can change that behavior: you must not use uninitialized variables, assign a default value (if you wish).
(and it is not stale content of compiler, but the random value read from the RAM).
I doubt it's a random value, it's more likely to be the hard-coded value from the previously compiled class which is still being executed because compilation failed (and thus didn't replace the class), and the custom run command does not make execution conditional on successful compilation.
And yes, you can change that behavior: you must not use uninitialized variables, assign a default value (if you wish).
You don't understand. Whether my code is right or wrong is not the point. I knew it was wrong when I wrote it. The point is I want Geany to print nothing besides the error message whenever there is a compilation error.
Quote:
Originally Posted by boughtonp
and the custom run command does not make execution conditional on successful compilation.
Can the custom command make execution conditional on successful compilation? Do you know how?
If boughtonp is right in post #3 you executed the compile and run in one step. Therefore the new compilation failed, the old code was executed again.
It looks like I missed it.
Code:
javac "%f"; java Main
So if you want to do it differently just do it differently.
Your LQ history shows you've been working with command line for well over a decade. Do you really need to be told how to do this?
(If so, maybe you should spend some time reviewing the Bash manual...)
OK, gumshoe. But tweaking that command line wasn't working the way I expected. A simple '&&' wouldn't do the trick.
So I had to write a script:
Code:
javac $1
output=`java Main`;
if test $? -eq 0; then
java Main
else
echo "ERROR"
fi
And it's not Bash, it's sh. I avoid Bash whenever possible because reasons.
It works, but I'm not very happy with the fact that I am running 'java Main' twice. Can that code be shortened?
Why the drama here? Why not just do your compile as step 1, and if successful, run it as step2? No need for shell scripts and such....
1) compile it
> javac abc.java
If successful then run it. Otherwise fix the problem and recompile.
2) run it
> java abc
Geany requires a one liner. Using a script gives me more lines. The first line will compile it but will not generate any output. It's the second line that generates it.
Geany requires a one liner. Using a script gives me more lines. The first line will compile it but will not generate any output. It's the second line that generates it.
Then I would suggest that you skip using the geany gui and do the compile from the command line so you are able to see any error messages. After a successful compile then use geany if you wish
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.