Is it possible to set the class path only when using javac but then run the program without specifying the class path at java?
For example,
I am using ant that does the compiling for me and it sets the class path to ./:lib/swingx-all-1.6.3.jar. As far as I know, this basically compiles/uses javac with that class path.
So is it possible to then run my program with just java MainApp, i.e. without having to use a class path variable? Or is it only possible to run my program by using the ant command again?
You can bundle your application in a jar file. Then include your classpath in manifest.
Here are Ant manuals for jar and manifest tasks.
Then, to run your application:
$ java -jar MainApp.jar
If you can use ant to run your program, look at the java task. Create a new target named "run" or something like that.
You don't need to set the classpath environment variable; you can always supply a classpath manually with the -cp option.
Related
I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files
When I change my CLASSPATH variable to JUNIT_HOME/junit-4.12.jar in system variables, my Java command stops working.
For example, when I want to execute a class file I get the error "Could not find or load main class", though javac is working fine.
When I remove the CLASSPATH, the java command starts working again.
The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. When you execute a java command to start a Java application, it start a Java runtime environment, loading a specified class, and calling that class's main method.
If your CLASSPATH variable is set to JUNIT_HOME/junit-4.12.jar, only classes inside the JUNIT_HOME/junit-4.12.jar will be loaded. Therefore, you will get a Could not find or load main class error.
The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications.
The default value of the class path is ".", meaning that only the current directory is searched. If you want also find classes file in other directory, say classes in c:\otherDirectory, you can set the class path to the following:
java -classpath ".;c:\otherDirectory"
Do you have more jars that need to go on the classpath?
JUNIT is for test purposes only and I would guess that you need to add all the dependencies you are using also on the classpath.
On the other hand it is not a good idea to do this through system environment variables. Please also look at the java -classpath command then you can add classpath dependencies for one application or command.
I'm new to java and I'm experimenting with hello world app.
I've exported the app from Eclipse into a jar, i DID specify the launch configuration, and when I ran it from command line, I retrieved
Error: Could not find or load main class
That can be fixed by specifying class path like this:
java -cp .:myjar.jar MyMainClass
However, this is really inconvenient. Is there any way, preferably through eclipse, to specify MyMainClass as a metadata inside the jar file so I don't have to write it down every time I launch the app?
yes, you can by writing a manifest file, and then running java -jar Yourjarfile.jar
I want to create our own jar which has some simple methods like a print method.
I created the jar of that java project and imported it into an other project I am building path also.
The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class.
i am using eclipse 3.4 (Eclipse Ganymede (3.4))version
Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.
To compile:
javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java
To Run:
java -cp .:PathToMyJar/MyJar.jar MyMainClass
The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.
If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.
Back again, this time with a java question. I was told how to get Processbuilder to run nonnative scripts (calling the program that would run the script), but I've been trying to run a java script and have run into a couple problems. First off, should I use a .class or .jar? both of these can be run but I'm not sure which one of them will work better. And then how do I execute them correctly? I've tried calling java (/usr/lib/jvm/java-6-openjdk/jre/bin/java) and then giving the filepath to the class file, but that doesn't seem to work.
Any ideas?
I agree with sarnold in terms of the .jar question. In terms of executing code using ProcessBuilder, you can execute a .jar file as long as this file contains a main() method, and has the Main-Class manifest header, which can be generated when the .jar is created. Once you have the .jar created, you'd use a command like this to run the .jar:
java -jar jar_file_name_here.jar
If you have multiple main classes and you want to run a specific one, you could use a command like this:
java -jar jar_name.jar com.main.class.package.path.here.SomeClassName
Are you trying to execute someone elses .jar, or is it one of your own that you just want to be executed inside a script? Why are you using a script, out of curiosity?
You either need -jar and the pathname of a jar that has a manifest that names your main class, or -cp with the pathname of a directory that has your classes in it in the standard layout, or -cp with the pathname of a jar followed by the name of the class with a main.
java -jar I_AM_A_JAR_WITH_A_MANIFEST.jar
java -cp I_AM_JAR_1.jar:I_AM_JAR2.jar... this.is.my.FooClass
java -cp dir_path1:dir_path2:dir_path3 this.is.my.FooClass
where the 'dir_pathN' is a dir with standard class hierarchy.