In a web-app I'm writing, the user is supposed to enter the path in the Perforce repository for the node they're entering. The application is supposed to validate that the entered directory exists in the repo.
I've got the P4Package (p4.jar) lib, and I'm configuring it correctly enough that it works for almost everything, EXCEPT this directory validation. I'm creating a DirEntry (from the p4.jar) using a configured Env and a path, but when I call DirEntry.sync(), it issues an incorrect command. Where I want it to issue the command:
p4 [config info] dirs directory_argument <-- using the dirs command to validate a dir
Instead, it issues:
p4 [config info] dirs directory_argument%1 <-- note extraneous %1
Which always fails, since none of the directories have a %1 at the end of them.
Any help? Is there a different way to check that a directory exists using this package?
Sounds like the sync command has a bug in relation to dir entries and the command. My suggestion would be to just roll the command yourself, using the perforce command line as that has to be set up anyway in order to use the java library.
Process p = Runtime.getRuntime().exec("p4 dirs " + directory_argument);
BufferedReader stdOut = new BufferedReader(new InputReader(p.InputStream()));
//Read the output of the command and process appropriately after this
I would try another library, P4Java, instead:
http://tek42.com/p4java
P4Java is much newer and I've found works much better than the P4Package. It is used in the Hudson project and I've seen it in the Fisheye source, though, I'm not sure if they are using it or not.
So, the code I was using did have a bug requiring me to make a change and check the code into my repository.
However, since then, Perforce has come up with their own Java wrapper for the P4 client which works much better. I'd give that one a shot.
Related
So I have this code that works fine, it launch the .jar file from another machine that I have configure in my pc as a red ubication
Runtime.getRuntime().exec("java -jar Z:\\AAA\\BBB\\CCC\\ZZZ.jar");
But now I want to launch the .jar from that external path without using the shortcut before (so I can launch it with this code in a machine that dont have that red ubication configured)
Runtime.getRuntime().exec("java -jar MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");
But doent work (I can access and open the file manually without a problem).
When I enter the java -jar MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar in the Command prompt it return me Error: Unable to access jarfile MMM\NNN, so perhaps one problem is that the path have a space in the folder name, but I think that may be something else too.
The question is, if the problem is the space, how I can solve it? I cant find a way. And in the other hand, how I can run it in another machine? I have to use that red ubication IP in some way instead?
PD: Using this code, it return me true
File f = new File("\\\\MMM\\NNN LLL\\OOO\\ZZZ.jar");
System.out.println(f.exists()); //--> true
So looks like the spaces dont interfere in the path (the four "\" doesnt seem to do anything in the tests when launching)
I have heard other people having such problems. The main reason for that is that probably Java exec method is not network (SMB) aware. So it doesn't even try to open from the network.
Anyway running the code like that from the network might not be the best solution. First of all the network might be unavailable, or the java file coming might be corrupted. If you want to do it properly you have several options:
Simple option that can work:
Create a bat file that works and exec that one - you can even copy the file locally first to make sure it is available first (if it is big and the network fails)
A better solution :
Use java to copy the file to the working directory and execute it from there.
A plus to do it like that by downloading is that you can maintain a version of the file (hash?) so you don't download it if it is the same. Or you can have fallback - execute the last downloaded version if the network drive is unavailable.
Use a local maven repository and dependency for that jar :)
This way it will keep the version of the jar locally and won't have to download it every time. It will also download a new version if available and the code will be more mainstream (for example not platform / pc dependent)
The answer give by #MadProgrammer works fine!
ProcessBuilder builder = new ProcessBuilder("java", "-jar", "MMM\\NNN LLL\\OOO\\AAA\\BBB\\CCC\\ZZZ.jar");
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
Lot of thanks! In any case going to check the ideas posted by Veselin Davidov
I tried googling a lot but couldnt get a proper working solution ..
directory consists of all java files and external jarfile(google.guava.jar).. i want to execute it in a batch file.. i have tried a lot of things...but still says deffclasserror.. can anyone help me out on how to make it work...(Windows)..
Structure looks like this:
Folder
--------jar file
--------java file
--------bat file
set path="C:\Program Files (x86)\Java\jdk1.8.0_73\bin"
javac -cp google.guava.jar convertohash
javac FinalOutput.java
java convertohash
java FinalOutput
pause
Try this:
"C:\Program Files (x86)\Java\jdk1.8.0_73\bin\java" -cp %YOUR_CLASSPATH%;%YOUR_CLASSPATH_REPORTS%;%EXTRA_LIB% -Djava.library.path=./dll your.main.class
Before this line you need to set up your YOUR_CLASSPATH your YOUR_CLASSPATH_REPORTS and EXTRA_LIB with = and separating the concurrences with ";" (without the ""). For example:
SET EXTRA_LIB=.\lib\mysql-connector-java.jar;.\lib\anotherlibrary.jar; etc
Being the "lib" folder the one were you store your libraries, the route doesn't stricly needs to be the one shown on the example just put the one were you store your libraries (if you are using some ofc).
Also keep in mind that if you are going to use this bat in several machines they must have the same jdk installed and on the same route specified or you'll need to change it manually because the application wont launch.
I just export my java project to executable jar. I have somehow made my project work to access value from DB (hibernate.cfg.xml), config.properties, and log4j.properties & editable for future...
I want to put hibernate.cf.xml, config.properties and log4j.properties in the outside of jar and place them somewhere in other directory.
I've been search for this and got this way :
Created a /path/to/mydir directory at some place in my deployement system.
Moved log4j.properties and META-INF and hibernate.properties and hibernate.cfg.xml to mydir.
and use this command
this is just example:
java -cp Myjar.jar:/path/to/mydir MyMainClass
the command that I write is like this:
java -cp coreservice.jar:/mon/properties CoreServiceController.java
I've try like that way but i got this error
Error: Could not find or load main class
anyone can help me to fix it or give the better way will be pleasure ~
====================(EDIT)===================================================
Oh my god ~
I have delete(cut) folder META-INF from my jar and paste to my config folder (/mon/properties)
and try this command again java -cp coreservice.jar;mon/properties/ id.co.bn
i.coreservice.controller.CoreServiceController..... annnndddddd this work ;))
tha-nks for Apostolos, Antoniossss, Yasa and no name :)
You should not use
java -cp coreservice.jar:/mon/properties CoreServiceController.java
you should put the class file there and try again, not the source java file
if this is in package let's say com.test, then in parent directory of com/test you type
java -cp coreservice.jar:. com.test.CoreServiceController.
this should work
(Edit) to load hibernate.cfg.xml file from specific location, try this method inside your HibernateUtil class:
public static void setConnectionProperties() {
URL configFileURL = null;
try {
configFileURL = URL_OF_YOUR_CFG_XML_FILE;
configuration = (new Configuration()).configure(configFileURL);
sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("Error while initializing hibernate: " + e.getMessage());
}
}
If I right understood your question:
When you configure sessionFactory you can write so
new Configuration()
.configure("/path/to/your/files/hibernate.cfg.xml")
.buildSessionFactory();
full example
So as you have still the same problem than I must say that you are doing something very very wrong, because this is the very basics of java runtime. Principles:
Definetly you have to provide fully qualified class name for java to run, not source file (*.java) so your call has to be proper.package.CoreServiceController
CoreServiceController cannot be in default page (no package name specified) - if it does, create some package just for test eg. service.core and put CoreServiceController there. As for point 1 - your call will be java -cp coreservice.jar:/mon/properties service.core.CoreServiceController nothing more, nothing else.
Java has nasty behaviour if you provide wrong entry for classpath - it does not say that eg. file myUberProgram.jar does not exists etc. It will just ignore such entry and go on. I had similar issues on Linux with CP - mispelled jar file and "what the hell is going on here?! Why is it not running" - same problem as you have - no class found
Check to be 101% sure, if there is CoreServiceController.class file in your jar file - maybe something wrong is with the packaging, and there is no such file in jar.
Finally, if all previous restrictions are fulfilled and nothing works, last chance for you, is to navigate to change working directory to one that jar file is in (simply go there from command line) and try tu run application without external cp provided. From directory where jar is:
Call java -cp Myjar.jar proper.package.CoreServiceController - should work but next one is 100% checked
Call java -cp ./Myjar.jar proper.package.CoreServiceController - all my applications on remote Linux server are lunched from bash scripts just like that and it always works.
If none of this helped you, than I must say dump your mashine into the ocean because it is cursed or something xD - I just want to say, that I have no other ideas how to help you.
For your practice build simple HelloWorld jar and try to lunch it from command line (but not runnable jar so you will have to provide classpath). Maybe in this way you will se what you did wrong.
My thinking is done for today, good day sir!
PS. #splatter_fadli I allowed myself to edit your question becouse as for now it is missguiding - you dont have problem with H configuration, only with running app while multiple entries in class path are provided. Maybe it will be accepted. - Aaaaaand it is done.
Last stand edit:
According to this question - QA put classpath entry into commas. Try it out. If Class not found will be gone, than we are half way there. -cp "coreservice.jar:/mon/properties"
Hi I am using Maven for Selenium automation tests using Java. I am using Excel Sheet to read data for filling up a registration form. Now It is a normal Maven archetype.
src
--main
--java
--mypackage for coding goes here.
--resources
--data
-- the excel sheet
--test
-- some stuff here under java
currently if I want to read a file I am writing src/main/resources/data/theexcelsheet.xlsx
but when I share this jar, I think this will break and I don't want this to break if I package this jar.
How do I make it platform or format independent.
I am using APACHE POI Api if you are thinking how I am reading files.
Someone reading gave me idea that I might be able to use MANIFEST files to do this but I am not sure, can someone help?
If you use getResource() to point to your file, the syntax of the path always uses / to separate the components, never \ so you do not have to worry.
In general, outside of a jar, for example if you want to save application settings in a folder, you have to use a OS specific separator - you can obtain it this way:
System.out.println("my" + File.separator + "dir");
Returns my\dir on Win and my/dir on Linux
Of course, that's not enough as you cannot use hardcoded paths like the "c:" drive, but you can get the most common paths reading the System class properties, eg:
System.out.println(System.getProperty("user.home"));
returns C:\Users\piero in Windows7 and /home/piero in Linux
I am trying to use IM4J (a Java wrapper for ImageMagick) to create thumbnails of JPEGs and it is my first experience (ever) with both libraries. Please note that this is a hard requirement handed to me by my tech lead (so please don't suggest to use anything other than an IM4J/ImageMagick) solution - my hands are tied on the technology choice here!
I am getting a FileNotFoundException on the and convert command which tells me I don't have one of these libraries (or both) setup correctly.
On my computer, here is my directory structure:
C:/
myApp/
images/ --> where all of my JPEGs are
thumbnails/ --> where I want ImageMagick to send the converted thumbnails to
imageMagickHome/ --> Where I downloaded the DLL to
ImageMagick-6.7.6-1-Q16-windows-dll.exe
...
In my Java project, I make sure that the IM4J JAR (im4java-1.2.0.jar) is on the classpath at runtime. Although I am required to use the 1.2.0 version of IM4J, I have the liberty to use any version of ImageMagick that I want. I simply chose this version because it seemed like the most current/stable version for my Windows 7 (32-bit) machine. If I should use a different version, please send me a link to it from the ImageMagick downloads page in your answer!
As for ImageMagick, I just downloaded that EXE from here and placed it in the folder mentioned above - I didn't do any installation, wizard, MSI, environment variable configuration, etc.
Then, in my Java code:
// In my driver...
File currentFile = new File("C:/myApp/images/test.jpg"); --> exists and is sitting at this location
File thumbFile = new File("C:/myApp/thumbnails/test-thumb.jpg"); --> doesnt exist yet! (destination file)
Thumbnailer myThumbnailer = new Thumbnailer();
myThumbnailer.generateThumbnail(currentFile, thumbFile);
// Then the Thumbnailer:
public class Thumbnailer
{
// ... omitted for brevity
public void generateThumbnail(File originalFile, File thumbnailFile)
{
// Reads appConfig.xml from classpath, validates it against a schema,
// and reads the contents of an element called <imPath> into this
// method's return value. See below
String imPath = getIMPathFromAppConfigFile();
org.im4java.core.IMOperation op = new Operation();
op.colorspace(this.colorSpace);
op.addImage(originalFile.getAbsolutePath());
op.flatten();
op.addImage(thumbnailFile.getAbsolutePath());
ConvertCmd cmd = new ConvertCmd();
cmd.setSearchPath(imPath);
// This next line is what throws the FileNotFoundException
cmd.run(op);
}
}
The section of my appConfig.xml file that contains the imPath:
<imPath>C:/myApp/imageMagickHome</imPath>
Please note - if this appConfig.xml is not well-formed, our schema validator will catch it. Since we are not getting schema validation errors, we can rule this out as a culprit. However, notice my file path delimiters; they are all forward slashes. I did this because I was told that, on Windows systems, the forward slash is treated the same as a *nix backslash, in reference to file paths. Believe it or not, we are developing on Windows
machines, but deploying to linux servers, so this was my solution (again, not my call!).
IM4J even acknowledges that Windows users can have trouble sometimes and explains in this article that Windows developers might have to set an IM4JAVA_TOOLPATH env var to get this library to work. I tried this suggestion, created a new System-wide environmental variable of the same name and set its value to C:\myApp\imageMagickHome. Still no difference. But notice here I am using backslashes. This is because this env var is local to my machine, whereas the appConfig.xml is a config descriptor that gets deployed to the linux servers.
From what I can tell, the culprit is probably one (or more) of the following:
I didn't "install" the ImageMagick EXE correctly and should have used an installer/MSI; or I need to add some other environmental variables for ImageMagick (not IM4J) itself
Perhaps I still don't have IM4J configured correctly and need to add more environmental variables
Could be the Windows/*nix "/" vs. "" issue from my appConfig.xml file as mentioned above
I'm also perplexed as to why I'm getting a FileNotFoundException on a file named "convert":
java.io.FileNotFoundException: convert
I assume this is a batch/shell file living somewhere inside the IM4J jar (since the only thing I downloaded for ImageMagick was the EXE). However, if I extract the IM4J jar I only see classes inside of it. I see "script generator" classes, so I assume these kick off before my cmd.run(op) call and create the convert file, and maybe that's what I'm missing (perhaps I need to manually kick off one of these generators, like CmdScriptGenerator prior to executing my Thumbnailer methods. . Or, maybe my download is incomplete.
Either way, I'm just not versed enough with either library to know where to start.
Thanks for any help with this.
Run the 'ImageMagick-6.7.6-1-Q16-windows-dll.exe' installer first to install the imagemagick libraries. Then make sure your environment path includes the location of the installed binaries ('convert.exe', 'mogrify.exe', etc)
Make sure u have Set the environment-variable IM4JAVA_TOOLPATH.