• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to run a java program from anywhere?  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've spent a couple hours on this and I've got other stuff to get done. I've got a java app with 3 source files, they live in the same directory. For testing I copy a data file from my working directory to my source directory and things run great. But if I move to my working directory and try to run the java program I can't do it.

I've made jar files, farked with classpath, copied jar files to my local directory, nothing has worked. What I have to do is "cd .../working/dir; cp .../java/src/*.class ." This is pretty much the definition of "wrong", "sucks" and "stupid".

Lets say my source code is in .../java/Workspace/Foo/src. I want to run that program from c:/Documents and Folders/whatever. How do I do that?

This is actually my java killer. C, C++, Pascal, ADA, Basic, awk, sed, tcl/tk, perl, python, whatever. You put the executable/script somewhere and you're done. In java, it's looking to be a major pain in the butt. If I can't easily "cd some/random/directory"; execute some/random/java/code" then I'm done with java.

This kinda works with a post I did yesterday about directory structure. It's not that I can't figure it out so much as I really don't like the direction it's leading me to.


 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. Pain in the butt. Java itself is platform independent but no platform has a native command shell that is Java aware. You end up having to copy your Java application files to a known directory and creating a platform DEpendent shell script to launch the Java application. Typically the shell script (.bat in the case of Windows) is only a single line. The location of your shell script should be in your path (e.g. the PATH environment variable in Windows). The script will need the location of your Java application files to be hard coded or you could use another environment variable to point to it. Once all this is in place you can run your Java application from any directory in the system.

Additional headaches:

All command shells parse the command line in various ways. One of the side effects is that if you want to pass a wildcard argument to your application, the shell gets a hold of it first and attempts to expand it, which, in my case, was not what I wanted. Another problem comes when you try to pass arguments enclosed in quotes. Quotes are usually handled by the shell which usually strips them off. I've occasionally toyed with the idea of writing my own jshell program that is Java aware.

Oracle has, in the recent 8u45 release, moved where the java programs are installed in Windows. Oracle has provided a set of .lnk links pointing to java.exe and javaw.exe. The problem with this is that Windows doesn't like links to exe files and you get the pop-up prompt: "what program would you like to use to handle this file type".
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I like the way Java does it, because it forces you to think about what you're really running, instead of the perpetual DLL hell you get with C/C++. It also allows you to easily plug in extensions to your application. These benefits will become more apparent after you become familiar with service providers.

Can you please explain in more detail what exactly isn't working?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What isn't working is I can't put my java/jar file somewhere in my PATH, then go to some/random/directory and run the java program on my data.

For every other language I've been able to make an executable, put that executable in my PATH, and run it. That's what I want to do with java. If I can't do that then java is useless to me.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, calm down.

Have you confirmed that you can execute your jar simply by double clicking it? Remember that a jar is not an actual executable. The jar's manifest must specify an entry point, and the .jar extension must be associated with a JRE.

Be more specific in what's not working. ItDoesntWorkIsUseless. How did you compile your code? How did you archive your code? How did you run your code? What error message are you getting?

You can do pretty much anything with Java you can with most other languages. Just have the patience to work out how.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this link for some ideas: https://stackoverflow.com/questions/2011664/compiling-a-java-program-into-an-exe.
For Perl (just to pick one of your examples) there is perl2exe which essentially creates a wrapper around your perl script with an exe launcher. Appears that the suggestions inn StackOverflow do a similar sort of thing.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Java I would very much prefer to use a shortcut which invokes something like 'java -jar app.jar'.
 
umar farooque
Greenhorn
Posts: 4
Opera Pulse IDE Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use bat file to start the java program from any where else but java program must be console based.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umar farooque wrote:but java program must be console based.


This is not true. You can execute javaw to run an application in windowed mode.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried a lot of things last night and got several different errors. Things like "can't find entry point", "can't find or load main class", even at one point it complained about the /' character needed to specify the path to the jar file.

Interpret "it doesn't work" as "I tried everything I and google could think of, they all failed in different ways".

I don't need a windowed mode, I'm parsing data files. Starting it with a .bat file is fine, I just want to be able to say "foo datafile", "java foo datafile", or "jar jarfile datafile" and have it run.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:I tried a lot of things last night and got several different errors. Things like "can't find entry point", "can't find or load main class", even at one point it complained about the /' character needed to specify the path to the jar file.

Interpret "it doesn't work" as "I tried everything I and google could think of, they all failed in different ways".

I don't need a windowed mode, I'm parsing data files. Starting it with a .bat file is fine, I just want to be able to say "foo datafile", "java foo datafile", or "jar jarfile datafile" and have it run.
Do you have a .bat file? Can we take a look at it?

"foo datafile" should work with an appropriate .bat file.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No bat file as I haven't yet figured out how to run the program. When I can run it I'll make a .bat file if I need one.

Unless of course by bat file you mean:

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My rudimentary approach:
My installs always go into the same directory tree, just to keep things simple.

C:\bin\<.bat file(s)>
C:\bin\<myapp>\<java files>


Typical .bat files (note: this uses Oracle's new java executable location).

C:\ProgramData\Oracle\Java\javapath\javaw.exe -classpath c:\bin\app_finance\finance.jar;c:\bin\app_finance\libs\csbutils.jar;c:\bin\app_finance\libs\jcalendar-1.4.jar finance.text.Finance

-or-

C:\ProgramData\Oracle\java\javapath\java.exe -jar C:\bin\myapp\myapp.jar %*

I added C:\bin to my PATH because that is where all of my .bat files reside.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\ProgramData\Oracle\java\javapath\java.exe -jar C:\bin\myapp\myapp.jar %*


This is one of the things I tried yesterday that didn't work. How did you generate the jar file?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:No bat file as I haven't yet figured out how to run the program. When I can run it I'll make a .bat file if I need one.

Unless of course by bat file you mean:

No, this is not what I mean by a bat file. For a proper bat file (i.e. "foo.bat"), you should be able to change directory to wherever "datafile" and "database" are located and just execute "foo datafile". No file copying involved.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:
C:\ProgramData\Oracle\java\javapath\java.exe -jar C:\bin\myapp\myapp.jar %*


This is one of the things I tried yesterday that didn't work. How did you generate the jar file?

Currently I generate (copy/paste/edit) the .bat files by hand.

This should have worked, assuming:
* The Oracle path is correct for your install of Java
* myapp.jar contains a manifest

If you have these things and it doesn't work I'd be interested in knowing what errors or behaviors you are seeing.

If you don't have a manifest you could try:

C:\ProgramData\Oracle\java\javapath\java.exe -classpath C:\bin\myapp\myapp.jar MyMain %*

 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of this seems like a classpath issue. Forget about jars for a while. If you run a .class file from a different folder than the default package folder (typically src), then you must specify a classpath. You need to run this:

java -cp <location of bin folder> com.example.app.Main

If this does not work, tell us the exact error message.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've sorta got it.

java -cp Foo.jar Foo inFile

I can't put Foo.jar anywhere but the current working directory, otherwise I get "Error: Could not find or load main class Foo". I don't know if that's a java or a cygwin problem.

The problem with this, of course, is that when I update Foo I have to be sure to pop Foo.jar files everywhere I want to run the program.

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:I've sorta got it.

java -cp Foo.jar Foo inFile

I can't put Foo.jar anywhere but the current working directory, otherwise I get "Error: Could not find or load main class Foo". I don't know if that's a java or a cygwin problem.

The problem with this, of course, is that when I update Foo I have to be sure to pop Foo.jar files everywhere I want to run the program.


No need to pop everywhere, just add an absolute path. E.g.

java -cp C:\bin\Foo.jar Foo inFile

Obviously you can put Foo.jar in any directory, 'bin' just happens to be where I put mine.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run cygwin, which doesn't use '\' in paths. Rather, it uses '/' like *nix does. I was trying "java -cp /tmp/Foo.jar Foo file" when I got the error "Error: Could not find or load main class". That's why I'm not sure if I've got a cygwin or a java issue.

cygwin is a *nix environment on Windows that works well 95% of the time.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:I can't put Foo.jar anywhere but the current working directory, otherwise I get "Error: Could not find or load main class Foo".


Well, that's to be expected, isn't it? It's basically your executable. Your shell needs a way to find it before it can run it. Having them in the current directory works. Using an absolute path works even better. Personally I think the most elegant solution is to distribute your application with a shortcut that uses a target 'java -jar <absolute path to jar>'. If you add the installation directory to the PATH, and your add the .lnk extension to the PATHEXT, you can run your application from anywhere by simply invoking the name of the shortcut.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for cygwin, I'm not sure how *nix uses shortcuts, or whether cygwin picks up Windows environment variables.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I say cygwin works well 95% of the time, for the other 5% paths are the problem 99% of the time. Environment variables aren't a problem except, of course, when the path issues crop up.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Personally I think the most elegant solution is to distribute your application with a shortcut that uses a target 'java -jar <absolute path to jar>'. If you add the installation directory to the PATH, and your add the .lnk extension to the PATHEXT, you can run your application from anywhere by simply invoking the name of the shortcut.
This is exactly what is wrong. I used to do this all the time and it worked great, UNTIL Oracle's 8u45 release. Now it asks me which program I want to use to open the link (provided by Oracle) to java.exe.

In my shortcut properties my 'target' is set to: C:\ProgramData\Oracle\Java\javapath\java.exe -classpath c:\bin\app_finance\finance.jar;c:\bin\app_finance\libs\csbutils.jar;c:\bin\app_finance\libs\jcalendar-.4.jar Finance

Note that 'java.exe' isn't really 'java.exe' but a link to it (not my doing, see Oracle).

Originally I didn't have ".LNK" in my PATHEXT but I added it and rebooted - same result.

Edit: Also tried just vanilla 'java.exe', but when I saved the shortcut and then reopened the long form was back again.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Could not find or load main class" is an entry point problem. If you run a jar, the jar needs to know where the main class is. You can do this by specifying it in a manifest file, or by archiving your application with jar.exe's -e switch.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood. My guess as to what's happening is java finds the jar file in /tmp, but then some internal program can't parse cygwin paths, and I get the error.

Haven't written it yet, but I suspect my .bat file (actually a sh file under cygwin) will be:

cp /my/java/development/dir/Foo/Foo.jar .
java -cp Foo.jar Foo $1
rm Foo.jar

I can then stuff that sh file in my ~/local/bin directory and all will be well.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So remove C:\ProgramData\Oracle\Java\javapath from the PATH and put the actual Java installation there instead.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the problem, and I mis-stated my suspicion. I suspect my shell finds /tmp/Foo.jar just fine, but java or one of it's internal parts can't parse cygwin paths.

Heh, got it. Using c:\\cygwin64\\tmp\\Foo.jar works fine.

I know I got a bit testy at times over the last 24 hours, but thanks for being patient with me and guiding me down the path to the solution.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:So remove C:\ProgramData\Oracle\Java\javapath from the PATH and put the actual Java installation there instead.

Not wanting to hijack this thread I created a new thread.
http://www.coderanch.com/t/651254/java/java/run-java-Windows-LNK-file#3008093
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Understood. My guess as to what's happening is java finds the jar file in /tmp, but then some internal program can't parse cygwin paths, and I get the error.

Haven't written it yet, but I suspect my .bat file (actually a sh file under cygwin) will be:

cp /my/java/development/dir/Foo/Foo.jar .
java -cp Foo.jar Foo $1
rm Foo.jar

I can then stuff that sh file in my ~/local/bin directory and all will be well.


You should not have to copy the jar every time you want to run it. This is how my make.bat looks on Windows:

And src/META-INF/MANIFEST.MF:
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:That's not the problem, and I mis-stated my suspicion. I suspect my shell finds /tmp/Foo.jar just fine, but java or one of it's internal parts can't parse cygwin paths.

Heh, got it. Using c:\\cygwin64\\tmp\\Foo.jar works fine.

I know I got a bit testy at times over the last 24 hours, but thanks for being patient with me and guiding me down the path to the solution.


Glad you worked it out
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!