If I'm trying to run a java program and I don't know the exact name of the Main class, is there any way to use tab completion to figure it out?
java -cp stackoverflow.jar org.<tab>
stackoverflow serverfault stackexchange
java -cp stackoverflow.jjar org.stackoverflow.<tab>
Main IntegrationTest QuestionAnswerConsole
Something along those lines.
Basically you are asking how to configure shell autocompetion to support java. It is possible. Take a look on this discussion: How does bash tab completion work?
I have to say that this is a good idea not only for discovering the main class but also to complete other command line options and a class path. I'd be glad to use such script if you develop it. Good luck.
EDIT
At least on my Ubuntu file less /usr/share/bash-completion/completions/java exists and therefore some completion should work. You are always welcome to improve the script.
The only way to get the Tab Completion that you are talking about is to use a shell that is "Java aware" or a shell script that provides this feature for the java command. git has a similar feature, so I don't think it's completely impossible.
Edit:
According to this question on SU, it is possible to create an autocompletion script for the bash shell. Since the question on SU is slightly different than what you are asking, I don't see a lot of specific details that relate to this question. However, it looks like a good place to start.
I don't know any easy way to get the autocompletion of classes names if they are hidden in a jar file.
On the other hand, you can add a Manifest to your jar to make it auto-executable (ie., you just have to run java -cp ... -jar stackoverflow.jar) !
In the jar archive, add a META-INF folder, and inside that folder, create a MANIFEST.MF file that reads :
Main-Class: org.stackoverflow.Main
(or whatever your main class really is).
Here is some documentation : Setting an application's Entrypoint
Other answers suggest you solutions but as a sidenote, you don't need it if you provide maintainer of the jar file provides a manifest file with path META-INF/MANIFEST.MF. Then java automatically extracts main class from the manifest and you can run it this way:
java -jar stackoverflow.jar
Related
In CMD I compile project with including libraries
java -cp app.jar;libs/*;. com.app.Main
and it works, but I want create BATCH script, which do exactly the same. I create test.bat and put code like below:
#ECHO off
java -cp app.jar;libs/*;. com.app.Main
PAUSE
But when I run the test.bat the CMD was shown and there is information "Error: Could not find or load main class com.app.Main".
BATCH script is located at the same folder as app.jar and libs folder.
What is wrong with this batch script?
Probably the characters on the compile line are significant to the batch interpreter; try putting the classpath in double quotes.
After Java 6, Classpaths could be built by using wildcard characters.
You can create a directory named classpath and put your JARs inside it. Then you can create your .bat file like this:
#ECHO off
java -cp .;classpath/* com.app.Main
pause
You should have a structure like this:
com
`---app
`---Main.java
classpath
`---your-crital-code-1.0.jar
compile.bat
I see that a lot of people are asking this question on StackOverflow, so here is a little tip for you guys.
I usually avoid using the cd command, as it may create some hassle. In windows, you can Shift + Right Click to open a command window in a particular directory.
I always prefer relative paths over absolutes, so that I don't get into hassle of managing long paths.
Here is a little program that deals with this kind of problem. You can always refer to and contribute to it so that we can make good examples for Java beginners :)
I created a GUI in the NetBeans and Then a jar file is generated. Now, when I click on a button to run the program with the jar file, there is no operation after clicking and the results that are tables and graphing data types did not show. How I can fix this problem.
Thanks.
There are different ways to export a program. You can use it as a java application or an appelet. You probably made it into an appelet when you really want a separate application.
But without you telling us how you made the jar file or what the program is, my guess is as good as anyones.
After you compile the program successfully, you can run it. Assuming that your program uses a standard look and feel — such as the Java, Windows, or GTK+ look and feel — you can use the interpreter to run the program without adding anything to your class path. For example:
java HelloWorldSwing
For programs that use a nonstandard look and feel or any other nonstandard code package, you must make sure that the necessary classes are in the class path. For example:
Solaris/Linux
java -classpath.:/home/me/lnfdir/newlnf.jar HelloWorldSwing
Microsoft Windows
java -classpath .;C:\java\lnfdir\newlnf.jar HelloWorldSwing
I think I have seen this done, but am not sure where. What I want to do is to create a bat file I can package with my class files when sending to a friend to show them progress/ask advice on non programming matters. My friend is not very handy when it comes to code and doesn't like changing computer settings. Just using java myClass as a command line won't work here because although my friend does have java installed, he has not set his windows environment variables so his command prompt knows where to find java.
What kind of line would I need to add to my batch file to make it so it can compensate for problems like this?
Create a manifest file (manifest.txt):
Main-Class: com.mycompany.myapp.MyMainClass
Package your app as a jar:
jar cfm myjarfile.jar manifest.txt *.class
Create a batch file:
start myjarfile.jar
If it is about sharing and running a single java file without jar dependencies. And you are only worried about the java runtime environment setup, then you can use online java code compilers and executors. Here is one:
http://javalaunch.com/JavaLaunch.jsp
You can google for more!
Use an IDE, NetBeans or eclipse and package your files as a Jar file.. that can be executed directly and you do not need to worry about dependencies, other classes or libraries.
Anyone who uses Java regularly knows the amount of trouble multiple jars in classpath can give us. I am looking for a tool which will help me -
Search for class files inside jars in Classpath
Search for class files inside jars in a specific location
Maybe too much to ask for but show me if the same class is present in multiple jars.
Also needless to say it should be fast. Most of the tools i have evaluated are very slow and not upto the mark.
Any tools for the above purpose greatly appreciated.
Currently i use WinRar achieve search(which BTW is really fast) to search for class files inside jars in a specific location. But there is no way to utilize that to search in Classpath.
If you use Bash this script may be helpful:
find . -name *.jar -exec bash -c "echo {} && jar tvf {} | grep MyClass" \;
If you are using eclipse, then IBM's classfinder may be useful. You can perform a (fuzzy) search for a class in a (set of) directories, and get all jar that contains such a class.
Not sure about the classpath option, however.
regards,
Guillaume
You can use jarbrowser for this
If you're using Eclipse, and they're on your classpath, you can CapitalCaseControlSpace what you want.
For example, if I want to get a new ApacheFanUtilityMethodizerFunctor (which everyone needs, of course) I simply type AFUMF and then Ctrl+Space and it populates it for me. Since it's already on my classpath, I don't need to hunt for it, because it's there.
Now, if it wasn't on my classpath, and I had to find what jar it was in for purposes of including it, then back to WinRAR (or for me, Google typically) I go.
My own solution for managing java jars files and java classpath is really simple. Firstly I create folder inside e.g.:
C:\Program Files\Java\libraries\
Here I put all my *.jar files which i need to use.
Then I modify the:
PATH(modyfy also CLASSPATH or JAVA_HOME)
system variable and add this path shown above.
Problem occurs when one class is in two diffrent jars but I managed my jar to avoid this problem.
Now when java is running jvm can find my classes and ClassNoFoundException isn't thrown.
A late response, but in case anyone searching has a similar problem, I wrote a simple tool a few years back that I use all the time for this sort of problem.
It's free to use and open source. If you find it useful, let me know, :-)
https://github.com/eurozulu/Findclass
I am working on a .jar file library to implement a bunch of helper classes to interface a PC to a piece of external hardware. I'll also add some simple applications, either command-line or GUI, to handle common tasks using the library.
My question is, is there a recommended way to simplify the command-line instantiation of a JVM in a certain specific way?
e.g. instead of requiring a user to type a cryptic error-prone command like:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar arg1 arg2
instead I do
TurboBlenderApp arg1 arg2
?
I suppose I could use shell scripts (incl. win32 Batch Files... gacckkk), it's just that I'm not good at those + I was wondering if there was a more straightforward platform-independent way of running a Java app from the commandline.
When you use -jar, then-cp (and the CLASSPATH variable) will be ignored
Just provide a executable jar. "java -jar TheApp <whateverargumentsyouwant>" shouldn't be too hard (you can have a Class-Path attribute in your jar files manifest, however).
if it is too hard write a GUI
or provide those shell scripts/batch files. Writing those isn't too hard either.
If you just want to simplify local execution, use a batch file or even just create a custom shortcut. If you want to create a launcher and package the executable jar and required libs for deployment, use something like Launch4j.
For some reason this trick just doesn't get around. What you need to do is to create a custom manifest for your jar file that defines the Main-class: property, and include in the jar file all the class files you're using. Then all you need is to run
$ java -jar myapp.jar
On real operating systems, you can even just run the jar file, as they will use the magic number to start it. But if you must, a one-liner batch or shell-script containing that line is all that's needed.
This is described in one of the Java Tutorials.
Shell scripts and batch files are the standard way of accomplishing what you want. (Look at any major Java product.)
This is, of course, absolutely pathetic and ridiculous. Go Java.
Your alternative is to write a little C program that does what you need (creates the java process) and compile that for each of your supported platforms. (C is the truly platform independent language).
Either way, you will need to take platform-dependent steps to make your app feel truly at home in the OS. On OS X, you will need to make a .app bundle, on Windows you need to package atleast the icon and version info into an EXE. For Linux, well, shell scripts suffice. :-)
If you use java -jar to launch your application, you can add additional jars to the classpath by adding a Class-Path entry to the main jar's manifest. See the jar file spec for more information.
Another solution is found in the Jakarta Commons, as always : commons-launcher.
A batch file would do the job. Put the exact command you want executed into a file myProg.bat and run it.
It's pretty easy:
Write this in TurboBlenderApp.cmd:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar %1 %2
// Bear in mind saua answer about -cp and -jar
Then from the command line you just write:
TurboBlenderApp arg1 arg2
Just the way you wanted.
If in Unix/Linux replace th4e %1 %2 with $1 $2 and make sure your app has execute rights ( If you don't know how to do this, I guess you don't need it either )