i have a ready to use .jar file and want to know if its possible to extract and rename the packages?
so when usually i start the .jar file with:
java -cp myFile.jar com.codehelper.demo.Main
i want to rename the "codehelper" in it to something different that i can run it by
java -cp myFile.jar com.NEW_NAME.demo.Main
i tried to decompile all files, add it to the folderstructure with renamed "codehelper" path and compile it again but it didnt work. i also renamed all the package includes in each file like
import com.codehelper...
so is my goal unreachable or can i do this? and if someone can explain me how to do, it will be very nice.
thank you and sory for my poor english
edit: it seems the only file i cant compile is a file containing this switch case.
private int priotiryLevel(DiscoveryInfoBehave info)
{
int ret = 0;
switch (1.$SwitchMap$com$peerialism$natcracker$common$GatewayDevice$GatewayType[info.getNatDevice().getGatewayType().ordinal()])
{
case 1:
ret = 0;
break;
case 2:
ret = 4;
break;
case 3:
ret = 5;
break;
}
return ret;
}
i tried also to rename the specific word inside this switch case but no effort.
Write a new wrapper class:
package com.NEW_NAME.demo;
public class Main
{
public static void main(String[] args)
{
com.codehelper.demo.Main.main(args);
}
}
compile it and add it to the jar. You can now invoke it as:
java -cp myFile.jar com.NEW_NAME.demo.Main
and it will silently dispatch to the real implementation.
This is not possible generally. If you rename some files, Java wont be able to find them (public class name must be same as file name). You can rename file with main class and call it as #Joe suggested in his answer. But if you rename somthing else, it will stop working. There could be cals to those classes in code. Same goes for the "codehelper" name. You can not remove it from code. Even if you remove it from one file, anyone will still be able to see this somewhere else in the code.
Rename directory is the same as rename file. You destroy namespace (package) and code will no longer work, because inside classes, this package is used. Plus there is no need to have import in the code, since you can call directly com.package-name.class from the executive code. By renaming package, you will destroy this and program will crash. It may run for a while, but once the program reach to this call, it will crash.
So this
import com.codehelper...
is not mandatory in the code, even if the code is using the package. You can write directly
com.codehelper.*** xy = new com.codehelper.***();
Even if you rename everything in the code, you still dont have guaranted functionality. Code may be using reflection and create class instances from sting code. For example see this:
Java how to instantiate a class from string
Under the line comment:
Doing some decompile -> compile work, is seems like code stealing, if you are not willing to pay licence and you want to hide it.
Plus doing something like this, it is ALWAYS a bad practice. I dont see any real use for this.
Related
I am trying to write a program that prints out a statement through Linux. The catch is that most of the statement must come from a method from a jar file. Basically. my code looks like this
public class identification {
public static void main(String[] args){ // Main method with print statement
System.out.print("I am " + person());
}
}
The person() method comes from the jar file. It should give something like "I am bob on a computer," but I can't quite figure out how to set up the CLASSPATH environment variable in the .bash_profile so it reads the method from the jar file. Any ideas? I am sure this is a simple task.
Thank you for your time.
use cp command to add a jar file
java -cp lib/myJar.jar myPackage.Program
also I believe you may need to import class where method person is in. I don't know how your hierarchy is so I can't help you with that.
You can add a jar to your classpath like this:
export CLASSPATH=$CLASSPATH:/path/to/your/myJar.jar
I need to execute a java jar file from Spoon.
The program has only one class, and all I want is to run it with or without parameters.
The class is named "Limpieza", and is inside a package named:
com.overflow.csv.clean
I have deploy the jar to:
C:\Program Files (x86)\Kettle\data-integration\lib
And from a Modified JavaScriptValue step, I am calling it this way:
var jar = com.everis.csv.clean.Limpieza;
This is not working at all, is there a way around to make it work?
Also would be nice to have a way to see the logs printed by the program when it runs.
I am not getting any error when I run the transformation.
Thanks.
Check the blog below:
https://anotherreeshu.wordpress.com/2015/02/07/using-external-jars-import-in-pentaho-data-integration/
Hope this might help :)
Spoon will load any jar files present in its
data-integration\lib
folder and its subfolders during startup, so if you want to access classes from a custom jar, you could place the jar here.
So you need to create a custom jar and place the jar in
data-integration\lib
location.
While calling a custom class in "Modified Java Script Value" or in "User Defined Java Class step" you should call with fully qualified name. For example var jar = com.everis.csv.clean.Limpieza.getInstance().getMyString();
Note: After placing the jar, make sure you restart the Spoon.
If still does not work please attach the Pentaho.log (data-integration-server/logs/Pentaho.log) and catalina.out(data-integration-server/tomcat/logs) logs
The answer was to create a User Defined Java Class (follow the guide Rishu pointed), and here is my working code:
import java.util.*;
import com.everis.csv.Cleaner;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
Cleaner c = new Cleaner();
c.clean();
// The rest of it is for making it work
// You will also need to make a Generate Rows step that inputs a row to this step.
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
r = createOutputRow(r, data.outputRowMeta.size());
putRow(data.outputRowMeta, r);
return true;
}
I can run programs which do not have a package without any hitch. If I try and add a package then java simply cannot find them. I have set the class path and I have tried running - java packagename.ProgramName.
I have found a number of similar threads on here and have spent four hours going through all of them and trying everything and nothing works for me.
Yet as soon as I edit the .java file and recompile without a package heading - it immediately works perfectly. Why? And how can I fix it? I would like to be able to have my classes organised in packages!
This is the code I am using (I normally use eclipse and just wrote this to try out cmd out of curiosity).
package hello;
public class HelloWorldApp{
public static void helloWorld(){
System.out.println("Hello world");
}
}
and
package hello;
public class HelloBackApp{
public static void helloBack(){
System.out.println("Hello back");
}
public static void main(String[] args){
HelloWorldApp.helloWorld();
helloBack();
}
}
As I say if I delete both the package heading java HelloBackApp runs just fine.
And my path to my program is
c:\Users\sam\javastuff\hello
I have of course tried java hello.HelloBackApp from both the javastuff dir and the hello dir. No joy
It works immediately if I delete both the package headings and type java HelloBackApp from the hello directory.
try as follows,
create folder structure as your package and place java file in that folder
For ex, my java file is under
c:\code\com\test\Test.java and package is "package com.test".
I compiled and run code from
c:\code>
c:\code> javac com\test\Test.java
c:\code> java com.test.Test
Ok after much research I realised what my problem was and have fully resolved it. I think I see why I have been unable to find an "answer" to this question in forums. It is not a simple quick fix - my whole understanding of how to correctly get the class path set up and get a proper compile done was very poor. It becomes a whole new subject if you switch from compiling/running on an IDE to doing so from the command line. I think it is an excellent thing for new programmers to do though as I believe the improved understanding of CLASSPATH is going to be something that will stand us all in good stead for the future.
I found all the answers to my questions here : http://www.ibm.com/developerworks/java/library/j-classpath-windows/
and recommend anyone having similar problems I was having to read through this excellent document. Best wishes to all the other guys struggling with this out there! :)
Is there a way to convert JAR lib into JAR standalone?
I need to find a standalone java executable that convert PDF into TIFF and I've found these JARs: http://www.icefaces.org/JForum/posts/list/17504.page
Any ideas?
Easiest might be to create another Jar with a Main() entry point, and then just use the java.exe executable to run it:
e.g.
> java.exe -cp MyJarMain.jar;MyPDFJar.jar com.mydomain.MyMain myPDF.pdf
Where MyMain is a class with a Main static method.
You'll need something with a main entry point to pass in and interpret some command line arguments (myPDF.pdf in my made-up example)
You could do an assembly (are you using maven?) and make sure the Main-Class entry in the manifest.mf points to the main class.
Since there is no main-Method, you have to write one, or write a whole new class to call the class/method TiffConver.convertPDF .
The question is, how you're going to use it. From the command line, you need no executable jar. From the Gui, maybe you want to pass a file to be converted by drag and drop? Then you should take the parameter(s) passed to main as Input-PDF-Names (if they end in .pdf) and pass the names iteratively to TiffConverter, for "a.pdf b.pdf" =>
TiffConver.convertPDF ("a.pdf", "a.tiff");
TiffConver.convertPDF ("b.pdf", "b.tiff");
TiffCoverter will silently overwrite existing tiffs, so check that before or change the code there - this is clearly bad habit, and look out for more such things - I didn't.
/*
* Remove target file if exists
*/
File f = new File(tif);
if (f.exists()) {
f.delete();
}
Maybe you wan't to write a swing-wrapper, which let's you choose Files interactively to be converted. This would be a nice idee, if no filename is given.
If the user passes "a.pdf xy.tiff" you could rename the converted file to xy, as additional feature.
Without a main-class, however, a standalone jar would be magic.
However, building a native executale is almost always a bad idea. You loose portability, you don't profit from security- and performance improvements to the JVM or fixed bugs. For multiple programs you need always an independend bugfix, which you might have to manage yourself, if you don't have a package-management as most linux distros have.
after clearing some questions:
public static void main (String [] args) {
if (args.length == 1 && args[0].endsWith (".pdf")) {
String target = args[0].replaceAll (".pdf$", ".tif");
convertPDF (args[0], target);
}
}
This method you put into TiffConvert. It will allow you to convert a simple pdf-File, and generate a tif-File with the same basename but ending in .tif, silently overwriting an existing one of the same name.
I guess you now need to know how to start it?
I'm having problems when trying to compile the following code:
I first tried compiling with this code:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
it works as it should. Now, if I try to replace the class name for any other name, it won't work anymore, as it seems to always look after HelloWorldApp. I made sure the file is being saved and so, I even reopened Eclipse. Still the same error. Maybe this is a common problem, with a small work-around?
Thanks
edit: I see what you guys mean, but why does it work when I have the file name as "Main.java" and a class name of "HelloWorldApp" ?
You need to rename your .java file to match the class name.
Eclipse will rename your .java file to match your class name automatically if you use its refactoring support. Right click on the class name, hover over Refactor, and select the Rename option. Now when you rename your class, Eclipse will automatically rename your .java file to match.
This is how Java works. Class must have the same name as the file. So the filename for the class MyApp, must be MyApp.java
Rename main as HelloWorldApp so that your app becomes HelloWorldApp.java.
When changing the name of your class use the REFACTOR option - If you try to edit the name manually yourself the Eclipse Project loses track of your objects.