So, I'm using JD-GUI to decompile some Java class files which doesn't have any documentation. This Java Application is running on a Windows Server and running via taskscheduler with a cmd file (I don't know exactly what, intern here).
If some one who worked with an args command can help me out, I would really appreciate it. The code snipped is attached, all what I want is to understand is what the for loop is doing at the end of the code.
public class Main
{
public static void main(String[] args)
{
try
{
List violationList = new ArrayList();
Calendar cal = Calendar.getInstance();
Date inputDate = new Date();
cal.setTime(inputDate);
cal.set(11, 0);
cal.set(12, 0);
cal.set(13, 1);
inputDate = cal.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
for (int ii = 0; ii < args.length; ii++) {
if (args[ii].equals("-date")) {
inputDate = dateFormat.parse(args[(ii + 1)]);
}
}
Here is the run.cmd file along with Java files I found. Notice there is .Main mention at the last line, something to do with those %1 and %2.
RUN.CMD
#ECHO OFF
set CP=.;realapplication.jar; commons- logging.jar;msbase.jar;mail.jar;activation.jar;log4j.jar;
set CP=%CP%sqljdbc4.jar;msutil.jar;spring-1.2.6.jar;commons-httpclient-2.0.1.jar;weblogic_8.1.jar;
set CP=%CP%zipfilemanager.jar;kmoscommon.jar;sessionpool.jar;kmosdao.jar;gendao.jar;rowset-1.0.1.jar;
java -classpath "%CP%" com.adnan.Main %1 %2
This for loop goes over the command line arguments and searches for a pair of arguments in the form of -date 16/11/81 (the given date is just an example, of course). Once it finds it, it parses the second argument (16/11/81, in this case) to a java.util.Date object and stores it in the inputDate variable. If the -date argument is omitted, today's date will be used.
Related
I have can not deal with execute my java method on oracle server. I load my java class on database using loadjava and create function using this query:
create or replace function getPeriodIdDay (multiplicity number, stardDate date, endDate date) return number
as language java
name 'Period.getPeriodIdDay(int, oracle.sql.Date, oracle.sql.Date) return int';
My method in class is:
public static int getPeriodIdDay(int multiplicity, DATE startDate, DATE date){
// day period calculation
int dayPeriodId = (date.dateValue().getTime() - startDate.dateValue().getTime()) / MILLIS_IN_DAY;
return dayPeriodId / multiplicity;
}
Each time when try to execute this function I have get this error:
29531. 00000 - "no method %s in class %s"
*Cause: An attempt was made to execute a non-existent method in a
Java class.
*Action: Adjust the call or create the specified method.
What am I doing wrong?
The signature of the java method in your function declaration is:
Period.getPeriodIdDay(int, oracle.sql.Date, oracle.sql.Date) return int
but the signatur of the metho in the java class itself is:
int getPeriodIdDay(int multiplicity, DATE startDate, DATE date)
If the capitalisation of DATE is no mistake when copying it here then this is a different type.
And even if it is a copy mistake: check with the imports of the java class that the same Date class is used.
the problem seems to be the DATE class, I suggest you to use String instead
You better change your method class like this:
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED JTESTDATE AS
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static int getDate(int multiplicity, String startDate,
String endDate) {
SimpleDateFormat sf = new SimpleDateFormat();
sf.applyPattern("yyyymmdd");//You Have to specify format here
Date sd;
try {
sd = sf.parse(startDate);
} catch (Exception e) {
return -1;
}
Date ed;
try {
ed = sf.parse(endDate);
} catch (Exception e) {
return -2;
}
long dayPeriodId = (ed.getTime() - sd.getTime()) / 24 * 60 * 60;//this is replaced with your code
return (int) (dayPeriodId / multiplicity);
}
}
I am trying to use the Date(int, int, int) constructor (per instructor requirements) and I am coming into some difficulty.
Initially I'm getting warnings because apparently this constructor is deprecated and additionally I am getting errors due to my usage of code.
I'll attach my code below. I tried using fileRead.nextInt() for the file Scanner and I also tried the method you see below with Integer.parseInt(fileRead.next()).
This is reading from a file that has text in the format:
firstName lastName, 4, 24, 2016, aStringOfTextPossiblyMultipleWords...
Where 4 is month, 24 is day, 2016 is year.
The errors I'm getting are...
Exception in thread "main" java.lang.NumberFormatException: For input string: " 4"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at BlogEntryTester.main(BlogEntryTester.java:59)
/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 6 seconds)
And here is the code. The error is during runtime near the end of the code.
import java.util.Date;
import java.util.Scanner;
import java.io.*;
public class BlogEntryTester {
/**
* #param args the command line arguments
*/
public static void main(String[] args){
Date newDate = new Date();
BlogEntry BE1 = new BlogEntry();
BlogEntry BE2 = new BlogEntry("firstName", newDate, "This is the body of "
+ "blog entry number two. This is the last sentence.");
BlogEntry BE3 = new BlogEntry(BE2);
BE1.setUsername("randFirstName");
BE1.setDateOfBlog(newDate);
BE1.setBlog("This is less than 10 words...");
System.out.println(BE1.toString());
System.out.println(BE2.toString());
System.out.println(BE3.toString());
Scanner keyboard = new Scanner(System.in);
Scanner fileRead = null;
String fileName;
System.out.print("Enter the name of the file you wish to read from: ");
fileName = keyboard.next();
try{
fileRead = new Scanner(new FileInputStream(fileName));
System.out.println("> File opened successfully.");
fileRead.useDelimiter(",|\\n");
}
catch(FileNotFoundException e){
System.out.println("> File not found.");
System.exit(0);
}
BlogEntry newBlog = new BlogEntry();
newBlog.setUsername(fileRead.next()); // Reads username from file.
if(newBlog.getUsername().length() > 20){
System.out.println("> Error: Username read from file exceeds 20 "
+ "characters.");
}
newBlog.setDateOfBlog(new Date(Integer.parseInt(fileRead.next()),
Integer.parseInt(fileRead.next()),
Integer.parseInt(fileRead.next())));
newBlog.setBlog(fileRead.next()); // Reads the text of the blog.
System.out.println(newBlog.toString()); // Prints the data gathered from file.
}
}
Trim whitespace
As comments said, you must trim the SPACE character appearing in front of your digit 4. You could call replace( " " , "" ) on the String. Or use the Google Guava library for clearing whitespace.
sql vs util
Be aware of the java.sql.Date class which is intended to represent a date-only value. In contrast the java.util.Date class you are using represents a date plus a time-of-day.
For a date-only value, the sql.Date class is more appropriate if you cannot use java.time framework described next. But also know that class is a bad hack, extending from util.Date while instructing you to ignore that fact of inheritance and to ignore its embedded time-of-day that is adjusted to 00:00:00 UTC. Confusing? Yes. These old date-time classes are a bloody mess.
java.time
You said your instructor is requiring the Date class, but you should know that class is notoriously troublesome and not recommended.
You are using an old outmoded class, java.util.Date, that has been supplanted by the java.time framework built into Java 8 and later.
Instead use LocalDate for a date-only value with no time-of-day and no time zone.
LocalDate localDate = LocalDate.of( 2016 , 4 , 24 );
I have simple test
#SuppressWarnings("deprecation")
#Test
public void test_NO_MILLIS() throws ParseException {
String rabbit = "22-OCT-15 06.37.35";
final String PATTERN = "dd-MMM-yy HH.mm.ss";
Date dateObject = new SimpleDateFormat(PATTERN).parse(rabbit);
Assert.assertNotNull(dateObject);
Assert.assertEquals(22, dateObject.getDate());
Assert.assertEquals(10, dateObject.getMonth() + 1);
Assert.assertEquals(2015, dateObject.getYear() + 1900);
Assert.assertEquals(6, dateObject.getHours());
Assert.assertEquals(37, dateObject.getMinutes());
Assert.assertEquals(35, dateObject.getSeconds());
}
And everything goes right. I get 22 as day in result.
But after I am adding microseconds both to pattern and to string value to be parsed
#Test
public void test_MILLIS() throws ParseException {
String rabbit = "22-OCT-15 06.37.35.586173000";
final String PATTERN = "dd-MMM-yy HH.mm.ss.SSSSSSSSS";
Date dateObject = new SimpleDateFormat(PATTERN).parse(rabbit);
Assert.assertNotNull(dateObject);
Assert.assertEquals(22, dateObject.getDate());
Assert.assertEquals(10, dateObject.getMonth() + 1);
Assert.assertEquals(2015, dateObject.getYear() + 1900);
Assert.assertEquals(6, dateObject.getHours());
Assert.assertEquals(37, dateObject.getMinutes());
Assert.assertEquals(35, dateObject.getSeconds());
}
I get an assert failure
junit.framework.AssertionFailedError: expected:<22> but was:<29>
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.Assert.failNotEquals(Assert.java:329)
at junit.framework.Assert.assertEquals(Assert.java:78)
at junit.framework.Assert.assertEquals(Assert.java:234)
at junit.framework.Assert.assertEquals(Assert.java:241)
at main.TestDateFormatTest.test_MILLIS(TestDateFormatTest.java:36)
...
Which means that day has become 29 instead of 22. What has gone wrong?
Tested
Platforms: mac osx 10.9, ubuntu, win7
jdk: 7,6
The format pattern S for milliseconds doesn't take into account mathematical placement values; it just sees 586173000 as the number of milliseconds to add to the rest of the date. That number is equivalent to about 6.784 days, so that explains why the date became 29 instead of 22.
Before parsing, cut off the milliseconds at 3 digits, e.g. "22-OCT-15 06.37.35.586", so it's interpreted as 586 milliseconds.
i am working on easy game(just for fun).
I have server in c and client in java.
I want get actual time on server and on client, but I can not come to the same results time.
On server i am using:
// the system time
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
// the current file time
FILETIME fileTime;
SystemTimeToFileTime(&systemTime, &fileTime);
// filetime in 100 nanosecond resolution
ULONGLONG fileTimeNano100;
fileTimeNano100 = (((ULONGLONG) fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
//to milliseconds and unix windows epoche offset removed
ULONGLONG posixTime = fileTimeNano100 / 10000 - 11644473600000;
return posixTime;
And i am getting time in format(output): 1750721123
On client i am using
long lDateTime = new Date().getTime();
System.out.println("Date() - Time in milliseconds: " + lDateTime);
Calendar lCDateTime = Calendar.getInstance();
System.out.println("Calender - Time in milliseconds :" + lCDateTime.getTimeInMillis());
And i am gettin format(output):
Calender - Time in milliseconds :1419089968022
Date() - Time in milliseconds: 1419089968022
Why? Where is the problem? How can i get the same TIME?
Both programs run on the same pc(win 8.1)
First of all. First block of code doesn't seem to be standard C code at all or rather you use some libraries that I just simply doesn't know.
There is no simple way to get actual time with less than a second accuracy in standard C. But here is the example with Java and C that actually works, so I hope this would help.
Java
package stackOverflow;
import java.util.Date;
public class Main {
public static void main(String[] args) {
long lDateTime = new Date().getTime();
System.out.println(lDateTime/1000);
}
}
Output: 1436200408
C
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<time.h>
int main(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
printf("%ld\n", tv.tv_sec);
return 0;
}
Output: 1436200418
I am trying the following code to open files in a certain directory. The name of the files are assigned by date but some dates are missing. I want to iterate through the dates to get the files and make the code go back 1 day every time it fails to find a file until it finally finds one (currentdate is a global variable and the strange xml element is because I'm using processing).
What I think the code should do is:
try to open the file with the given date.
on error, it goes to catch and gets a new date.
the process is repeated until a valid date is found.
when a valid date is found it goes to the line where break is and exits the loop.
But for some reason it does weird stuff like EDIT # sometimes it jumps too much, especially near the first month #
Is my logic not working for some reason?
Thanks
String strdate=getdatestring(counter);
int counter=0;
while(true){
try{
xmldata = new XMLElement(this, "dir/" + strdate + "_filename.xml" );
break;
}catch(NullPointerException e){
counter +=1;
strdate=getdatestring(counter);
}}
String getdatestring(int counter) {
Date firstdate=new Date();
int daystosum=0;
String strcurrentdate="";
if(keyPressed && key=='7'){
daystosum=-7;
}
daystosum=daystosum-counter;
Calendar c=Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try{
firstdate=formatter.parse("2012-04-13");//first day of the database
}catch(ParseException e){
println(e);
}
c.setTime(currentdate);
c.add(Calendar.DATE,daystosum);
currentdate=c.getTime();
if(currentdate.before(firstdate)){
currentdate=firstdate;
}
strcurrentdate=formatter.format(currentdate);
return strcurrentdate;
}
I believe once you do this,
daystosum=daystosum-counter;
you need to reset the counter as
counter = 0;
otherwise next time it will subtract more bigger number e.g. to start, say daystosum is 0 and counter is 5, after the daystosum=daystosum-counter;, daystosum will become -5. Again you go in the while loop and file is not found then count will increase to 6. In that case you would be getting `daystosum=daystosum-counter; as -5-6 = -11, but you would want it to move to -6. Resetting the counter should ix your issue.
On the other note, I think you can list down the files using file.listFiles() from the parent directory and perform the search on the file names. In that case, you are not attempting to open files again and again.