B4J Question Name and path of current executable

Didier9

Well-Known Member
Licensed User
Longtime User
This is slightly different from this:


I have packaged my B4J application as an exe, and it is not a console app. Is there a way to get the name of the executable from running code? (in case the user would have renamed it)

TIA
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The main exe file starts another process, which is the actual app. This means that there isn't a very good way to do it.
I don't know why you need it, but you have two options:
- Find the executables in File.Parent(File.DirApp)
- Use B4JPackager11 source code and modify the C# code that creates the launcher app to pass the executable name to the java process. It can be done as an argument or a SystemProperty.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Thank you Erel.

Reason for that is a little complicated. Details below if you have time to waste. I won't be offended if you skip ;) (it helps me to write it down, sometimes I realize the silliness of what I try to do)

I am looking at other ways to achieve the end goal, so hopefully one of those will pan out.

<details>
This is for a piece of software that controls serial devices. It needs COM port and baud rate info for the device. We often run multiple instances of the program on the same machine with different settings talking to different devices simultaneously I do save the settings to a map file so each time I start the software, it comes up with the last settings so when you start the second instance, you have to manually change the settings. I am evaluating different ways to be able to start the program with different defaults.

I implemented command line arguments to change baud rate and com port so I (emphasis on "I") can easily create a batch file that calls the program with the right settings and put it on the desktop. However it has been proven that creating the batch file was too arcane for most of my users (surprise surprise...) and we have dozens of those, so I thought of having the software create the batch file from the current settings (through a menu option) For that, it needs the path to the executable, which is not a constant because there are different versions of the software with various capabilities (and a different exe), and different path depending on how the machine is configured (Windows and stupid drive letters). I am trying to avoid people running the exe from the local drive so everything is done through the network. The path can be quite complex in our corporate network environment, a real rabbit hole...

So creating the batch file from the program would require the user to manually enter the name and path to the executable, I would prefer to avoid that but it is an option.
</details>


Thank you.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
So it's rather a "where do I store the config data" than to get the path of the executable. What about a SQlite DB (on a defined drive of course). I assume every user has a username or ID. You can get it very easy by using a vbs script (via jShell) and store the configs in that db under the username/ID. Or use simple maps which you store with the configs in it.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Well, this particular project is a workhorse used for testing our products via RS-485. I started with QuickBasic 4.5 (remember that?) under DOS (must have been in the 80's), then over the years I wrote versions for VB 4.0, VB 5.0, VB 6.0 (still being used), QuickC (a Microsoft product), gcc under Linux (still being used also since you can do so much fun stuff under Linux) and now B4J. So there is history and I have tried a bunch of variations over the years.

All versions before B4J allowed saving the main settings file (a map file) anywhere the user wanted. That file contains a lot of settings in addition to the serial port info.
Time has proven that was not a good idea. Users were saving those files everywhere and some settings will definitely affect the ability of the product to pass test, so since B4J makes it easy to store that kind of stuff in a file that is not readily accessible (since Windows hides it by default), I decided to use that and it is definitely an improvement over the previous system.

Turns out that thanks to Windows handling of COM ports, the COM port definitely needs to be adjustable by the user since a given USB/RS-485 adapter can show up as any COM port number.
So I came up with the command line parameters, which make it really easy for me to create a batch file with COM port data and a couple other tidbits and use that to start another instance of the program tailored for a different target. *My* problem is solved.

In most cases, the people in the Test department only need to run one instance of the program at a time and the settings will not change for a while, so in those cases it works very well too. I am now trying to address the less common situation where a particular user (that's not me) needs to have that capability but he/she is not enough familiar with batch files to create one that works and will not mess something else, (and yes, the whole thing is fully documented but as you know, nobody reads manuals) so I thought the program could generate the batch file using the current settings and put it on the desktop and Bob's your uncle. There really are only 4 settings that are controlled via command line parameters, everything else is still controlled through the main file.

About remote database and other fun stuff, you have to realize we are a US based defense contractor and the restrictions now placed by the government and our company policies on what we can (and mostly cannot) do over the network, or even on the local machine are absolutely mind blowing. The layers of red tape I would have to go through before being allowed to do that are such that I will be retired before that happens (to be honest, I am not that far from that point anyways :)
If you have heard about Bit9 (yes, the "security" company that was hacked in a spectacular way a couple of years ago allowing hackers to distribute malware along their "security" products), I don't need to say more. Otherwise, you always have Google :)

Besides, the settings that I want to save this way are essentially local and machine dependent (what piece of hardware you are actually trying to control and what the local COM port settings are), the settings that I do not want to change that way could be in a central database or common network location, but some machines do not have network access for a variety of reasons.
 
Last edited:
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Create a UI config app and have a save as option to allow you save different file names. You can use maps and write them to file. Launch your jar file and pass the file name.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Thank you John. Even though I would have to do some research before attempting to write that code, it sounds conceptually close to what I wanted to do and would not require any external resource.
Need to think about it.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
One thing I used to do under Linux was creating links to the executable under different names, and the program would behave differently depending on the name of the link that was used to start it. But that also requires being able to get the name of the executable.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
I would much rather write code one time than use a text editor multiple times :)
This was actually the point of the original question: how to have the program write a batch file for me.

It looks like the solution (for me) is to make sure the executable always has the same name and is in the path. In my environment, it is not hard to control that.
 
Upvote 0
Top