[SOLVED] How does a program "find" another file or program when it calls one?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
How does a program "find" another file or program when it calls one?
If the question doesn't tip you off as to how little I know, I don't know what will. Having said that, I'm trying to figure out how any program can call for a file, any file, when it runs. I know that my Linux system sometimes squawks when I enter something in bash without giving it a file path. And while I've gotten the examples that I've copied from Java tutorials to compile, when I read something saying that Java applets can be imported by a html script, the examples always leave out (or my mind can't grab) where and how the connection happens. I know this has got to sound strange to anyone with any kind of experience, but all I see is "There's this applet file, and it can work in a web page." I need some more info.
Is the applet, let's say I want to copy the Wkipedia example:http://en.wikipedia.org/wiki/Java_applet
and say I call the file something that the compiler can turn into something that runs. Then I guess I create a html page and somehow get the page to load or run in Firefox...I guess. Who knows? And does the applet come from anywhere on the web, or will it just work on my computer, and nowhere else?
And then there's the idea that almost every file I've installed has called on other files, especially libraries. When they install, I guess ./compile, make and make install commands make it so that I don't have to know how it works, but when you program, how does tab A find slot B when that's what's supposed to happen?
Sorry if this is dumb, I just haven't seen a good explanation of how all these files and programs connect.
If the question doesn't tip you off as to how little I know, I don't know what will. Having said that, I'm trying to figure out how any program can call for a file, any file, when it runs. I know that my Linux system sometimes squawks when I enter something in bash without giving it a file path. And while I've gotten the examples that I've copied from Java tutorials to compile, when I read something saying that Java applets can be imported by a html script, the examples always leave out (or my mind can't grab) where and how the connection happens. I know this has got to sound strange to anyone with any kind of experience, but all I see is "There's this applet file, and it can work in a web page." I need some more info.
Is the applet, let's say I want to copy the Wkipedia example:http://en.wikipedia.org/wiki/Java_applet
and say I call the file something that the compiler can turn into something that runs. Then I guess I create a html page and somehow get the page to load or run in Firefox...I guess. Who knows? And does the applet come from anywhere on the web, or will it just work on my computer, and nowhere else?
And then there's the idea that almost every file I've installed has called on other files, especially libraries. When they install, I guess ./compile, make and make install commands make it so that I don't have to know how it works, but when you program, how does tab A find slot B when that's what's supposed to happen?
Sorry if this is dumb, I just haven't seen a good explanation of how all these files and programs connect.
Thanks for the links, I just "skimmed" them but they look like they answer some of what I need.
I'm still stuck on the applets, or whatever, accessing via the web. The picture I have is that the html page can call "http.www.whatever" via <src>. But how or where is the applet? I use the img src a lot when I want to put something into a page, usually one where I just type into a form, as I've never created a web page. But I haven't made anything that generates a url to refer to, so I'm fuzzy about how a html page gets made.
Wait. You refer to three different things, that maybe remotely share a common aspect: "they hide some functionality" and thus some stuff seems transparent to you or to other things. Such aspect is embedded in everything, software or hardware and its called "abstraction".
Aside from that, the three things are unrelated:
The first one is how you type a command/program/script name in a command line and stuff gets executed. That has to do with the shell (the program that shows that command line and accepts your input) and a named value the shell pays attention to: its name is PATH and you refer to it as $PATH when you want to print it (type echo $PATH). The value is just a list of paths separated by colons. The shell looks for the program you want to run in these paths. Usually it will contain the common paths were programs are stored in Linux: /bin, /sbin, /usr/bin and /usr/sbin, but there are other locations too and you can add as you like more directories, and the executables (programs or scripts) stored there won't have to be invoked with a path. By the way, you can see where a program is located in your system with the command "which" (for example "which ls").
If you used the "echo $PATH" command before, the shell didn't even had to check $PATH to find the "echo" command. This is because "echo" is not a standalone program, stored in a file somewhere. It's an internal shell command, called a "builtin". You can see a list of builtins typing "help", and "help somebuiltin" will give you extra help on that builtin (I'm assuming you are using the the-facto shell in Linux: Bash).
So this is how the shell does its magic and runs something with you only typing a word. It ABSTRACTS you from knowing where a program is stored, and it abstracts you from the fact that some are binary programs, some are scripts, and some others are builtin commands (in fact it's a bit more complicated, this is just an overlook).
Second thing: Java
The support to embed Java (not Javascript, that is another lang) programs in HTML has to be provided by the browser, usually with the support of a browser plugin for Java, which implements the Java runtime and libraries. If you don't have this plugin your browser won't be able to do anything with a Java applet and you won't be able to use it.
How the hell is that you "embed Java in HTML" ?? Actually nothing is "embedded" in HTML as HTML is TEXT. BUT, in HTML you can "refer" to all kinds of objects. You can say "at this point render this image, look is a png and is at that computer, accesible by this protocol, and is located in this path at that computer" as your browser reads these references, it will connect again to where it's necessary and ask for stuff. Stuff is called "a resource".
The same way you refer to images, you refer to Java applets stored somewhere. The browser then gets the applet, and providing it has a Java plugin it hands over the applet to the plugin, and the plugin executes the applet.
Now, the plugin is a program running in your computer (which is running another program: the applet). The plugin has access to your *real environment* (operating system and libraries) as any other program, and it provides a *virtual environment* for the Java applet, which is *always the same*. This is how Java applets can run in many computers/operating systems: they don't operate with the real environment, as a normal program. They operate with the Java environment. Theres a level of indirection. There's a LEVEL OF ABSTRACTION, that allows Java applets to work everywhere a plugin for Java is able to function, hence they are subject to be transferred over networks between heterogeneous systems.
Note that the same accounts for flash programs, or Javascript programs, or any other "program content" to be distributed via web: as long as your browser has some component able to "interpret and run" the content, it can be delivered an will run in your system. The browser is then providing OS and hardware abstraction to these programs (at the cost of lots of CPU power sometimes...*cough*flash*cough*silverlight*cough*)
Third thing: system libraries and communication between programs
Well, these are two different things. And the way you put it you seem like saying "I want to know how programs interact with each other". The answer is: in a gazillion ways. So to end these neverending post I will refer only to system libraries, and in short tell you that you are on track when you guess that "./configure ; make" somehow links a program to the libraries it needs.
Abstraction again: those two commands do an awful lot of things, like checking you have the needed libraries and versions of them correctly installed in your system, configuring options and features for the program you are about to compile, compiling the program and LINKING it to the libraries it uses. Linking can be "static", when the functions that the program uses from the libraries are included into the program when its finished, or "dynamic" when only a REFERENCE to the function/library is stored in the program.
When the shell runs a binary program, it really asks the Linux Kernel to execute it, and the Linux kernel is who sets up the environment needed for the program to run, loads it into its own memory area, and transfers the control of the CPU to the first instruction of the program. But before doing so, it also runs a special code that it loads along with the program: this code in turn looks up what needed functions and libraries are used by the program, and loads them too, and after this the program is ready to run. Another way of using dynamic libraries is by a program explicitly asking for some function from some library.
Note that there is a tradeoff: with statically linked programs you get a fat executable with all the code in it. It doesn't depend on anything to load and run. Dynamically linked code instead is lightweight, but at the cost of being slightly slower during run time because of the time and cpu used to look up what's needed, find it, load it and arrange the references so the program is able to finally use the god damn functions. There are techniques to mitigate this penalties too (I won't describe them though).
So again, these are abstractions so a program can more or less say "I need X function" and the kernel can say "allright lemme get it for ya".
These are awfully gross simplifications of very big/complex subjects so you can aprehend more or less what they are about, of course. But, it's something.
Dinner calls. It's my price for having written such a gigantic post. I promess I'll never do this again in my life.
Wow! Not only is this one of the best answers I've ever recieved to a qusetion, it's like a wonderland of answers. Thanks isn't a big enough word in this case!
It seems to have a lot of what I haven't learned yet, and I'll be going through it for a while, I'm sure. But you gave me a ton of new "scope" that I hadn't digested yet, and I'm sure I'll have more to ask at some point.
Still foggy on how to make a simple applet (on my computer) and display it on a HTML file, again on my computer. I use nano to write the .java file, and everything I've read seems to indicate that I can use nano to make the HTML page too, but how would I load that HTML file to, say, Firefox?
Hey, glad I've managed to help you get on track! It was that or confusing you even more, so I'm just happy it turned out right.
Here's an article on embedding Java applets, I don't use Java so I can't help you more with that. But typing Java in a text editor won't get you the applet, you should compile it.
That said, I personally can't recommend that you start learning Java these days. It's far more useful (and less booooooooooooooooriiiiiiiiiiiiiing) to learn HTML5/CSS3/Javascript to develop for browsers, and Apache+PHP/Python/Ruby/WhateverLanguage and some database for server side web development. IMHO, Java is more or less going the way of the dodo, or at least being more and more relegated to ultra enterprise niches, where (I hope) eventually will be overthrown too.
I use nano to write the .java file, and everything I've read seems to indicate that I can use nano to make the HTML page too, but how would I load that HTML file to, say, Firefox?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.