Java and the Win API

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
Hello,

To finish a Java program I'm writing, I think that I need to call on the Windows API.

I'm assuming that the API gives developers a clean interface to the windows environment.

What I want to do exactly is open a window that will prompt the user to enter a file from non-volatile memory to be uploaded so that I can instantiate a new object whose instance variable will be the file that the user uploaded.

We've all seen the upload window that I'm talking about. It's the same window that pops up whenever you upload anything to the internet.

How do I get my Java program to call on the Windows API to do this?

I'm running Windows 7. Do I automatically have the Windows API on my machine as part of the OS, or is this something that I need to download?

In general, how would you interact with any API using any program written in an arbitrary language?

Thanks.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I'm assuming that the API gives developers a clean interface to the windows environment.

You may not think so after you use it.

The "Windows API" can cover a lot of things. The most basic API that most developers might use is the C-callable Win32 API. At this level the API provides methods to open and manipulate files, but it does not provide user interface components.

The next level up towards a working UI is what's known as the common controls. This is a set of window classes and controls that work with the Win32 API's window creation services. They are not easy to use, but if you want to have a look this is a decent place to start:

http://support.microsoft.com/kb/161286

Most people do not compose GUI interfaces at this level anymore, because doing so is pretty damn tedious. You'll get very familiar with C structs, Windows classes, the message pump, etc. It's a lot of plumbing.

Instead you can use MFC if you're writing native C++. MFC provides a somewhat easier way to deal with the GUI layer of the application. It abstracts things like window classes and the message pump so you don't have to worry about them. You create controls and manipulate them and use a designer to compose them into an application. Still far from trivial, but easier than Win32.

http://www.functionx.com/visualc/cpp/Lesson01.htm

MFC was brutal enough, and it's foundation on the Win32 API messy enough, that Microsoft embarked on creating a whole new way for applications to interact with the system and each other, and this is the .NET framework that you've probably heard about. If the earth is the Win32 API, and low-earth orbit is MFC then .NET is somewhere out by the moon. You don't think about any of those system level concerns. You just create objects and manipulate them.

.NET provides two main ways to create native GUI applications: Windows Forms and Windows Presentation Foundation. They're both huge topics in their own right.

So, to finally answer your question: any of these approaches will allow you to open a file dialog :).
 

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
MFC was brutal enough, and it's foundation on the Win32 API messy enough, that Microsoft embarked on creating a whole new way for applications to interact with the system and each other, and this is the .NET framework that you've probably heard about. If the earth is the Win32 API, and low-earth orbit is MFC then .NET is somewhere out by the moon. You don't think about any of those system level concerns. You just create objects and manipulate them.

Yes, I have .NET 4.5.1. I downloaded them as part of the updates that Microsoft keeps sending (I think).

But that was before I started learning about computers. I assumed back then that the .NET framework was just some environment that Windows needed to operate or something. I didn't know that it was for developers.

.NET provides two main ways to create native GUI applications: Windows Forms and Windows Presentation Foundation. They're both huge topics in their own right.

Awesome. But how do I interact with the framework? I've had it on my machine for quite some time now, but I've never "called out to it".

How do I get that code into my Java code? Is there some way to seamlessly incorporate these .NET calls into my Java code?

Do I write a separate .NET code file and then link the two together?

Generally speaking, how do you link code written in a language to an environment?

Thanks.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Awesome. But how do I interact with the framework? I've had it on my machine for quite some time now, but I've never "called out to it".

You don't and beginner's answer is the one you want. I got sidetracked by your mention of the Win32 API. In general it's the same answer: you build your application using the language the framework supports, and the tools the framework supports, and you call the APIs that the framework provides. In your case you'll be calling into the JVM, not the Win32 API. Java is a cross-platform environment that hides those specific details from you.
 

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
you build your application using the language the framework supports,

OK. So, because .NET comes from Microsoft, these languages would be C#, Visual Basic, Visual C++, etc. Right?

and the tools the framework supports, and you call the APIs that the framework provides.

Wait. This is beginning to sound like a runtime environment. Is the .NET framework just the Microsoft analogue to the Java runtime environment complete with predefined methods, etc.?

I think you need to look into native methods and make some wrapper dlls

Native methods? Wrappers? I have a looooot to learn......
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
OK. So, because .NET comes from Microsoft, these languages would be C#, Visual Basic, Visual C++, etc. Right?

Wait. This is beginning to sound like a runtime environment. Is the .NET framework just the Microsoft analogue to the Java runtime environment complete with predefined methods, etc.?

Yes, and essentially yes.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
Good grief, if you are using Java you should be using Swing to make UIs, not some crazy wrappers around raw win32.

Edit: Also, in this and many of your other questions you are WAY too eager to dive into low level details that are far above your experience level and frankly not useful to the overwhelming majority of developers. When you need to do something like this don't dive off the deep end and start thinking about all the details, instead think of the most simple question that describes your need ("how do you make a user interface in java?") and google it.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Good grief, if you are using Java you should be using Swing to make UIs, not some crazy wrappers around raw win32.

SWT... :D

If you can chance the immaturity of it, I kind of like JavaFx better than swing for UI work.
 

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
Also, in this and many of your other questions you are WAY too eager to dive into low level details that are far above your experience level and frankly not useful to the overwhelming majority of developers.

Sorry. Personality flaw I guess. If I don't know everything about something from top to bottom, I get anxious and irritated.

In the end, it retards the learning process. But it's hard for me to control.

I think that I have an anxiety disorder.......

Also, regarding swing, I'm very new and had no idea that Java had a class that would create the upload / save window for Windows OS.

I thought that I needed to somehow interact with the OS directly and make it pop up. That's why I thought of the API.

I find Java aggravating because there are so many predefined elements. With over 2,000 classes, I feel like I'm looking for a needle in a stack of needles whenever I need a method.
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
I find Java aggravating because there are so many predefined elements. With over 2,000 classes, I feel like I'm looking for a needle in a stack of needles whenever I need a method.

Like I said, think about about what you want to do instead of how to do it. It's pretty unlikely that you're going to try to do anything that hasn't been done before, you just have to ask google the right questions.

Also, you should be using an IDE, which will help with finding methods. I prefer Netbeans, but Eclipse and Intellij are also popular.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Like I said, think about about what you want to do instead of how to do it. It's pretty unlikely that you're going to try to do anything that hasn't been done before, you just have to ask google the right questions.

Also, you should be using an IDE, which will help with finding methods. I prefer Netbeans, but Eclipse and Intellij are also popular.

I agree. Most of my job is based around knowing when and how to ask google and stackoverflow for answers to my questions. It is a skill in its own right.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Edit: Also, in this and many of your other questions you are WAY too eager to dive into low level details that are far above your experience level and frankly not useful to the overwhelming majority of developers

Probably true in the literal sense, but hardly needs to be discouraged. That's how I learned. Of course... Carter was president.